Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yavgel85/d111d8a64f9d326edacabd7af96a7105 to your computer and use it in GitHub Desktop.
Save yavgel85/d111d8a64f9d326edacabd7af96a7105 to your computer and use it in GitHub Desktop.
Laravel validation after hook in form request file #laravel #validation
<?php
// if you want to run custom validation having a long function in laravel request file, you can use the following tricks to get the things done using withValidator method.
class StoreBlogPost extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}
//this method will be called automatically after basic validation and this is laravel's built in function
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment