8

So, I have the following rules in my model:

public static $rules = array(
    'name'            => 'required|alpha_dash|unique:subsidiaries',
    'internal_number' => 'required|alpha_dash|unique:subsidiaries',
    'expedition_rate' => array('required', 'regex:/^[0-9]{1,3}(\.?)[0-9]{1,2}$/'),
    'hundred_kg_rate' => array('regex:/^[0-9]{1,5}(\.?)[0-9]{1,2}$/'),
    'handling'        => array('regex:/^[0-9]{1,3}(\.?)[0-9]{1,2}$/'),
    'insurance'       => 'required|numeric',
);

But, for some reason, when the regex is applied in the pattern attribute tag in html... it breaks!

Result:

<input required="true" pattern="^[0-9]{" class="form-control" ....>
                               _________
                                 \
                                  => This right here should be
                               ^[0-9]{1,3}(\.?)[0-9]{1,2}$
3
  • Where are you outputting it, though? Are you using Former, by any chance? Commented Sep 23, 2013 at 17:18
  • @Raphael_ Actually, I am using Former... the rules are passed on from the controller to the view Commented Sep 27, 2013 at 21:55
  • Then this is really an issue with Former, not Laravel. Commented Sep 28, 2013 at 20:48

2 Answers 2

2

Try using like this.

array(
    'field' => 'regex:[a-z]'
);

Hope this would helpful.

Sign up to request clarification or add additional context in comments.

Comments

0

Try escaping the curly braces... I bet PHP is expecting to parse a variable inside, like this:

$var = 'something';
echo 'The value is {$var}.';
// Outputs:
// The value is something.

Contrast this with:

$var = 'something';
echo 'The value is \{$var\}.';
// Outputs:
// The value is $var.

The part that confuses me though, is that these are within single quotes, and so this shouldn't happen. Maybe a Laravel thing?

7 Comments

How can one, escape curly braces in a regex syntax? This is new for me
Double all of the backslashes.
Sorry, I think I misunderstood your question. To escape the curly brace, you should just be able to do \{...\}. When it renders, it shouldn't have that backslash.
That's weird. You may want to ask this question over at Laravel's GitHub... It might very well be a bug. I looked at their validation test cases, and none of them include curly braces: github.com/laravel/framework/blob/…
Interesting. In your case, rather than write a custom validator, you could instead just rewrite your RegEx to not include that comma. For example, x{1,3} can also be represented as xx?x?. At any rate, good luck with your project!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.