8.5 Custom Validation Rules
The validator allows you add extra rules to suit the needs of your application, let’s jump right in
and take a look at how we register a new validation rule.
3 Validator::register('superdooper', function($attribute, $value, $parameters\
5 return $value == 'superdooper';
Our newly created validation rule superdooper will ensure that our value matches the string
‘superdooper’. Your custom validations should return true on success, or false on failure.
The $attribute value will be the name of the field being validated, and $value will of course
contain the value.
The $parameters attribute contains an array of parameters that have been passed to the rule
after the colon, and separated by commas.
As you have created a new validator, there will be no error messages associated with it yet, we
will need to add one so that Laravel knows what to say when it fails. We can add an error
message in the same way as we have previously..
3 'superdooper' => 'The :attribute must be superdooper, ok trooper?!',
Once again you can pass the extra error message array as a third parameter to the Validator::
make() method, or simply add it to your application/language/en/validation.php file
for safe keeping.