Created
March 26, 2021 08:28
-
-
Save yavgel85/d111d8a64f9d326edacabd7af96a7105 to your computer and use it in GitHub Desktop.
Laravel validation after hook in form request file #laravel #validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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