Laravel is built with useful validation rules like required, numeric, unique data from a table in a database, etc. But there are some situations where the built-in validation rules cannot handle. That’s where extending Laravel’s validation class comes in handy. In this post I’ll be showing you how to extend Laravel’s validation class. This will enable you to use custom validation rules specifically created for your needs.
One thing I’ve noticed while evaluating Laravel is that it doesn’t have the methods that deals with an array of data. For example if you have this form:
1 2 3 4 5 6 7
And you want every student name to be required. Using Laravel’s built in methods. You do something like this:
1 2 3 4 5 6 7 8 9 10 11
But if you might have already noticed. This doesn’t work.
Curiosity leads us to inspecting Laravel’s validation method for the required rule (
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
As you can see from the above method. It doesn’t seem that Laravel is automatically dealing with array as inputs for form. Looking closer this block of code seems to be close to what were looking for. But this only deals with file input(stored in
$_FILES). So this isn’t the method were looking for.
1 2 3 4 5 6
Thankfully the creators of Laravel made it incredibly easy to extend its validation class in case a need like this arises. To extend Laravel’s validation class we’ll need to create a new library at
Let’s name it
1 2 3 4 5
Then disable the autoloading of the validator class on
1 2 3 4
Then we create a method that will deal with array inputs:
1 2 3 4 5 6 7 8
Naturally laravel’s input class would be able to capture any kind of input be it a regular string, file, or array.
Methods for the validation class accepts 3 arguments:
- Attribute – the name given to the input (Eg. name, age)
- Value – the value of the input (Eg. Yael, 27)
- Parameters – additional data passed after defining the rules.
For example the additional data that you pass after defining a
1 2 3 4 5
The additional data that I’m referrring to is the string that comes after the colon (tbl_users,username). In the case of defining a unique rule for a particular field. The first parameter is the name of the table, the second parameter is the name of the field.
The important thing to note here is that parameters are separated by comma.
And that method names has a prefix of
Ok back to the
Basically what the
arrayfull() method does is to check whether an array contains empty values
by using the
in_array() method. The
in_array() method simply returns
true if it has found a specific value in the array and
false if it didn’t find the value:
One more thing I’ve noticed about Laravel is its date validation rules. It only has rules for before and after a certain date. This is good but there should also be a validation rule for simply validating if the user input is indeed a date. So I’ve written a method that does it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
As you can see were counting the number fragments after invoking the
Anything other than 3 is not a valid number of fragments since the expected date format is supposed
to have 3 fragments(Year-Month-Day).
Once it passes this condition we then check if the date is a valid date in the Gregorian calendar
by invoking the
checkdate() function which accepts 3 arguments(month, day, year).
Default Validation Messages
You can just set the validation messages when creating an instance of the validator class like this:
1 2 3 4 5 6 7 8 9
But if you want to set default messages for your
validation rules then you can set it on
1 2 3 4 5
The key will be the name of the validation method.
For example if the name of your validation method is
validate_date then the key will be
The value will be the error message that you want to show up when there’s an error with the validation.
:attribute represents the name of the field.