Created
July 25, 2017 16:17
-
-
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
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 | |
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