Laravel Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|
Lessons:-Handling Input

8.6 Validation Classes
If we want to provide many new validation methods, or reuse them across a number if projects, it
would be best to create a validation class. Validation classes extend Laravel’s data, and overload it
with additional validation methods. The class is created in the application/libraries directory
for easy loading, but you could place it elsewhere as long as it is registered with the Autoloader
(later chapter). Let’s take a look at the class.1 <?php
2
3 // application/libraries/validator.php
4
5 class Validator extends Laravel\Validator {
6
7 public function validate_awesome($attribute, $value, $parameters)
8 {
9 return $value == 'awesome';
10 }
11
12 }
As you can see our Validator class extends the Laravel\Validator name-spaced core class and
provides additional validations in the form of validate_<rulename> methods. The validation
methods accept the same parameters as the Validator::register() closure, and work in the
same way.
In order to use our new validation class, we will need to remove the existing alias for Validator
from our application/config/application.php file. This way Laravel will use our created
class instead of the one in the Laravel source folder.
You could use this method to replace the original validation methods with your own, for example
you could create a validate_size method and calculate the size in an alternate format.
I would suggest adding custom error messages to the validation language file when using
Validation classes, this will allow for a much easier migration to another project, and will not
require any ‘source-digging’ to find all the messages used.

 

 

 
 
 

osdyui

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)