Created
April 18, 2015 03:52
-
-
Save vjrngn/ae2a3754e2d113e5b766 to your computer and use it in GitHub Desktop.
Easy Form Validation with Laravel
This file contains hidden or 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 Hotelier\FormValidation; | |
use Illuminate\Validation\Factory as LaravelValidator; | |
abstract class Validator { | |
/** | |
* Laravel's validator instance | |
* | |
* @var Validator | |
*/ | |
protected $validator; | |
/** | |
* Validation instance | |
*/ | |
protected $validation; | |
/** | |
* Instantiate Laravel's validator. | |
* | |
* @param LaravelValidator $validator | |
*/ | |
function __construct(LaravelValidator $validator) | |
{ | |
$this->validator = $validator; | |
} | |
/** | |
* Validate the form data | |
* | |
* @param $formData | |
* | |
* @return bool | |
* @throws FormValidationException | |
*/ | |
public function validate($formData) | |
{ | |
$this->validation = $this->validator->make( $formData, $this->getValidationRules() ); | |
if( $this->validation->fails() ) | |
{ | |
throw new FormValidationException( 'Validation Failed', $this->getValidationErrors() ); | |
} | |
return true; | |
} | |
/** | |
* Rules to validate the form data | |
* | |
* @return mixed | |
*/ | |
protected function getValidationRules() | |
{ | |
return $this->rules; | |
} | |
/** | |
* Validation errors | |
* | |
* @return mixed | |
*/ | |
protected function getValidationErrors() | |
{ | |
return $this->validation->errors(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment