dwightwatson / validating

Automatically validating Eloquent models for Laravel
MIT License
968 stars 76 forks source link

custom validator #201

Closed maxdiable closed 6 years ago

maxdiable commented 6 years ago

it's possible to create a custom validator, any example ??

br Max

dwightwatson commented 6 years ago

I don’t understand the question, could you give some more context as to what you’re trying to achieve?

maxdiable commented 6 years ago

hi, I would like to create a custom validation, for example I would like to check that a precise code is selected in an input field of the page, it's possible ? br Max

dwightwatson commented 6 years ago

Yes, just write a custom validation rule. The Laravel docs show you how to do this. Then just add your rule to the rules array on the model. This library just uses Laravel’s validation under the hood. Let me know if you have any issues with this.

maxdiable commented 6 years ago

I tried to understand how to make a custom rule and associate it with the rules of your plugin, but I can not figure out how to do it, I will try to do other tests. I created a custom rule for uppercase, and I was thinking of doing this: 'targa' => 'required | uppercase', but I think I'm wrong to do so br Max

dwightwatson commented 6 years ago

I'm not sure how more I can help apart from writing the code for you. Look at this page to see how to write custom validators for Laravel - using classes or extensions (you can't use the closure syntax with this package at this stage): https://laravel.com/docs/5.6/validation#custom-validation-rules

Then you just add those new rules to the $rules array on the model you want to validate. It would work just the same as if you were using Laravel's validator by itself.

dwightwatson commented 6 years ago

Actually it's possible that only the validator extensions work at the moment (not classes or closure syntax: #200).