0

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

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

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

4

1 回答 1

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 的验证中定义这些。但无论如何,我希望这会有所帮助!

于 2014-09-22T14:40:12.763 回答