Laravel Classroom image

Anil  Bist / Professional / Web Technology

 
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.

 

 

 
 
 
image
Anil  Bist

Skills    Laravel

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)