Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An extension of TrimmedFormRequest.php.Makes requests validate only and does not submit after validation passes but rather returns a json response
<?php
namespace App\Http\Requests;
use Illuminate\Validation\ValidationException;
/**
* Description of AjaxValidatableFormRequest
*
* @author msalisu
*/
class AjaxValidatableFormRequest extends TrimmedFormRequest
{
public function validate()
{
$this->prepareForValidation();
$instance = $this->getValidatorInstance();
if (! $this->passesAuthorization()) {
$this->failedAuthorization();
} elseif (! $instance->passes()) {
$this->failedValidation($instance);
}
if($this->ajax() && $this->has('validate_only')){
throw new ValidationException($instance,$this->validationResponse());
}
}
public function validationResponse(){
return response()->json(['code' => '000']);
}
/**
* Get data to be validated from the request.
*
* @return array
*/
protected function validationData()
{
return $this->all();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.