Skip to content

Instantly share code, notes, and snippets.

@msbrime
Created July 25, 2017 16:17
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 msbrime/d0ede8e89b26a1e6d16cbe7716c6a4af to your computer and use it in GitHub Desktop.
Save msbrime/d0ede8e89b26a1e6d16cbe7716c6a4af to your computer and use it in GitHub Desktop.
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