问题
I am using confide for user authentication. I wanna use custom rules using laravel's Validator (for firstname and lastname) class as in
$validator = Validator::make($user, $this->rules[$ruleset]);
and check with $validator->passes()
. How can I achieve this?
回答1:
First create your custom validation class. Something like this:
class CustomValidator
{
/**
* @return bool
*/
public function validateFirstname($attribute, $value, $parameters) {
{
// Do here your Firstname validation
...
}
/**
* @return bool
*/
public function validateLastname($attribute, $value, $parameters) {
{
// Do here your Lastname validation
...
}
}
Then register the new validation rules. For example at bootstrap/start.php
file add:
Validator::extend('firstname', 'CustomValidator@validateFirstname');
Validator::extend('lastname', 'CustomValidator@validateLastname');
Then you can use the new rules firtsname
and lastname
in your models.
I hope it works fine for you.
来源:https://stackoverflow.com/questions/26788297/confide-custom-validator