Creating two seperate validation messages for the same attribute

Viewed 48

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',
        ];
    }
2 Answers

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

i don't know if i understand your question correctly, but are you looking for something like this ?

public function rules()
{
    return $this->messages("end_time", [
        "after_or_equal",
        "after_or_equal:5:00",
    ]);
}

public function messages(string $Key, array $CustomAttributes)
{
    $Exceptions = [
        "end_time" => [
            "after_or_equal" => "Message 1",
            "after_or_equal:5:00" => "Message 2"
        ]
    ];
    $Exception = [
        $Key => []
    ];
    foreach ($CustomAttributes as $Attribute) {
        array_push($Exception[$Key], $Exceptions[$Key][$Attribute]);
    }
    return $Exception;
}
Related