A lightweight library to make your data validation easy, with Laravel PHP Framework.
Make your models self validating, just define the validation rules in your Eloquent model. Whenever you try to create or update data with your model and if validation fails, an exception will be thrown.
Add this to require section (in your composer.json):
protectedstatic$rules = [
'title' => 'required|min:5|max:50', // `title` is the field in your table'description' => 'required|min:5|max:500',
Now every time a validation rule breaks a Dryval\ValidationException will be thrown.
To catch this put the code below in render() method of your app/Exceptions/Handler.php.
It will look like this:
Now a user signsup with firstname.lastname@example.org which is not already in database, that's fine. But now what if the user is trying to update his profile with some changes but same email address? Yes, validation will fail.
To solve this problem Laravel accepts the 3rd param for unique rule. DRYVal makes it even easier just use :id: placeholder as 3rd param, its like a dynamic id. Example:
DRYVal throws Dryval\ValidationException when validation fails.
You can catch this exception app wise as shown in the first example or you can catch this in your controller/class. The getMessages()(plural) method of this exception class will return the validation error messages (MessageBag) same as Laravel's $validator->messages(). More here.
You can also throw this exception anywhere you need.