I am using Laravel 5.7 and i have many custom validation requirement.I have searched many tutorial but i found below two ways in latest version
First method is adding rule inline like
use Illuminate\Validation\Rule;
Validator::make($data, [
'zones' => [
'required',
Rule::in(['first-zone', 'second-zone']),
],
]);
and second method is extending validation but in this method i need to create instance of that class but it looks not good when we have many custom validation
lass Uppercase implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be uppercase.';
}
}
and one more method is adding validation in service provider .this is best way but i have many custom validation so it might grow more larger in service provider
Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
return $value == 'foo';
});
Last one i found old answer in stackoverflow
this is the best one i found compare to other. Now my question is any good way to create custom validation in latest laravel version
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire