Laravel 5.2 - validation rule does not work

1

I need to validate a zip field, but the following attempts do not work:

$rules= ['cep'  => 'required|numeric|size:8'];
ou
$rules= ['cep'  => 'required|numeric|min:8'];
ou
$rules= ['cep'  => 'numeric|size:8'];

The following validations work:

$rules= ['cep'  => 'required|numeric'];
ou
$rules= ['cep'  => 'required|size:8'];

Does anyone have a clue why it does not work?

    
asked by anonymous 05.09.2016 / 21:12

1 answer

2

zip can start with 0 , example 01415000 , so it can not have numeric and size:8 together and will never have eight numbers if you start with 0 .

So the ideal for validation would be digits : 8 : This validation works by verifying that all are numbers and that the number quantity is equal to 8 including 0.

$rules= ['cep'  => 'required|digits:8'];

If you want to create a validation with your own code follow the example below:

Registering a validation:

Validator::extend('cep', function($attribute, $value, $parameters, $validator) {
    return mb_strlen($value) === 8 && preg_match('/^(\d){8}$/', $value)
});
$rules= ['cep'  => 'required|cep'];

In this respect, validation will work your way. In this link , the validations are the Laravel has for version 5.2 and 5.3 so far.

    
06.09.2016 / 18:48