Search code examples
laravellaravel-validation

Custom validate message in Service


I have rule in my service:

public function rules()
{
   $data = [
    'companyDetailsINN'=>'required|digits_between:10,12|unique:accounts,companyDetailsINN',//todo добавить проверку по на кол-во символов в зависимости от typeID  (DEFAccountCompanyType, поле innDigits)
                    'companyDetailsINN.unique' => 'this is my custom error message for required',
           ];
}

But I want return custom validation message for error unique?


Solution

  • In case you're not using Form Request I'd suggest you to use it.

    Inside that, you could have your rules and messages. This way you could separate the validation completely from the controller.

    https://laravel.com/docs/7.x/validation#form-request-validation

    You could define custom error messages like this.

    public function messages()
    {
        return [
            'companyDetailsINN.unique' => 'this is my custom error message for unique',
        ];
    }
    

    https://laravel.com/docs/7.x/validation#customizing-the-error-messages