0

I use Laravel and have validate code in Request:

class InformationsRequest extends Request
{
    // validate rule
    public function rules()
    {
        return [
            'title' => 'required|max:2000',
            'description' => 'required|max:5000'
        ];
    }

    // custom message
    public function messages()
    {
        return [
            'title.required' => 'aaa',
            'description.required' => 'bbb',
        ];
    }
}

Today, please help me 2 points:

  1. How I make user-defined validate. (checkXXX method)

Method checkXXX maybe has 1 param, 2 param .....

public function rules()
{
    return [
        'title' => 'required|max:2000|checkXXX',
        'description' => 'required|max:5000'
    ];
}
  1. Custom message for checkXXX method
Tiến Marion
  • 109
  • 1
  • 5

1 Answers1

0

You can add custom validator rules in a service provider. There's more information on this in the docs.

Validator::extend('checkXXX', function ($attribute, $value, $parameters, $validator) {
    return $attribute === true;
});

Simply return false from the callback if the attribute fails validation.

Then, provide the custom message in either your lang files, or return one from the messages() method.

return [
    'title.checkXXX' => 'The :field failed the checkXXX rule.'
];
Dwight
  • 12,120
  • 6
  • 51
  • 64