表单请求按字段而不是按类型验证消息

时间:2016-07-07 23:51:25

标签: php validation laravel-5.2

假设在给定的表单中,您有两个date类型的字段:生日驾驶执照。现在,我想让Laravel使用以下规则处理日期验证:

<?php
public function rules() {
    return [
        'birthday' => 'required|date',
        'driver_license_date' => 'required|date'
    ];
}

但我想要这样的自定义消息:

<?php
public function messages() {
    return [
        'birthday.required' => 'Please fill up your birthday',
        'driver_license_date.required' => 
            'Please fill up the date that your Driver License was issued', 
        'birthday.invalid' => 
            'Your birthday date is invalid', // <-- This doesn't work
        'driver_license_date.invalid' => 
            'The date for the driver license provided is invalid', // <-- This doesn't work


        'date' => 'Invalid date', // <-- This works, but its one message for both fields
    ];
}

我知道:attribute魔术变量,虽然这是一个虚构的场景案例,但我有一个合法的问题,我需要验证相同类型的两个相互无关的字段。我希望能够为我的用户提供一条消息,我希望消息特定于每个字段,而不是类型的通用消息。

有可能实现这个目标吗?

1 个答案:

答案 0 :(得分:1)

您可以使用:

public function messages() {
    return [
        'birthday.required' => 'Please fill up your birthday',
        'driver_license_date.required' => 'Please fill up the date that your Driver License was issued', 
        'birthday.date' => 'Your birthday date is invalid',
        'driver_license_date.date' => 'The date for the driver license provided is invalid'
    ];
}