Search code examples
phplaravelvalidationlaravel-8laravel-validation

Creating two seperate validation messages for the same attribute


I'm trying to create two separate validation messages for the same validation attribute. There are two rules that use "before_or_equal" - the end_time has to be "before_or_equal" start_time and also the end_time has to be after 5:00 (time). The validation works, but I can't seem to find a way to create a working custom message for the latter.

I tried to specify the rule by including it literally with the value, but it doesn't seem to work.

This is what the custom request validation looks like for the end_time at the moment.

    public function rules()
    {
        return [
            'end_time' => ['after_or_equal:start_time', 'after_or_equal:5:00'],
        ];
    }

    public function messages()
    {
        return [
            'end_time.after_or_equal'     => 'Message 1',
            'end_time.after_or_equal:5:00'    => 'Message 2',
        ];
    }

Solution

  • You can use :date for your custom error messages.

    Example:

        public function rules()
        {
            return [
                'end_time' => ['after_or_equal:start_time', 'after_or_equal:5:00'],
            ];
        }
    
        public function messages()
        {
            return [
                'end_time.after_or_equal' => 'the :attribute time must be after :date',
            ];
        }
    
    

    The replaced value is actual value of first input of the validator