Skip to content

Instantly share code, notes, and snippets.

@sworup
Created July 31, 2016 07:00
Show Gist options
  • Save sworup/46af4ecdf4ebe542615f1568dfab1e76 to your computer and use it in GitHub Desktop.
Save sworup/46af4ecdf4ebe542615f1568dfab1e76 to your computer and use it in GitHub Desktop.
Laravel- Add after hook in request file. Reference: https://goo.gl/8BhhCB
<?php
/**
* Get the validator instance for the request and
* add attach callbacks to be run after validation
* is completed.
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function getValidatorInstance()
{
return parent::getValidatorInstance()->after(function($validator){
// Call the after method of the FormRequest (see below)
$this->after($validator);
});
}
/**
* Attach callbacks to be run after validation is completed.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return callback
*/
public function after($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