Ardent验证消息中的自定义属性名称

时间:2014-09-22 09:36:17

标签: laravel laravel-4 ardent laravel-validation

我在Laravel项目中使用Ardent因为我喜欢它非常棒的模型验证功能。现在我需要在验证错误消息中设置人类可理解的属性值。 我读到可以通过在validation.php文件(lang文件夹)中设置$ attributes数组来完成。

由于我来自Yii,我想知道是否有一种方法可以在模型基础上指定属性名称,而不是在validation.php lang文件中全局指定。

例如,如果我有两个模型,Employee和Company,都有“name”属性,我想将“name”属性的显示值分别设置为“Employee name”和“Company name”。 / p>

1 个答案:

答案 0 :(得分:0)

试一试。它来自GitHub上的文档。我从来没有和Ardent一起工作过,但它实际上有很好的写作。

class Employee extends \LaravelBook\Ardent\Ardent {
    public static $rules = array(
        'name' => 'Required|Alpha_Dash'
    );
    public static $customMessages = array(
        'required' => 'The Employee :attribute field is required.',
        'alpha_dash' => 'The Employee :attribute field must be Letters and Dashes only.'
    );
}

由于这些自定义消息是在Employees类中定义的,因此它们仅适用于此类的输入。您可以为Company类做同样的事情:

class Company extends \LaravelBook\Ardent\Ardent {
    public static $rules = array(
        'name' => 'Required|Alpha_Dash'
    );
    public static $customMessages = array(
        'required' => 'The Company :attribute field is required.',
        'alpha_dash' => 'The Company :attribute field must be Letters and Dashes only.'
    );
}

我认为这应该有效。但是,我没有办法测试它。另一个选项是使用自定义规则,例如

'name' => 'Employee_Required|Employee_Aplha_Dash'

在Laravel的验证中定义那些。但无论如何,我希望有所帮助!