我在我的Laravel项目中使用Ardent,因为我喜欢它的可怕的模型验证功能。现在,我需要在验证错误消息中设置人工可理解的属性值。我读到,它可以通过在$attributes文件(lang文件夹)中设置validation.php数组来实现。
由于我来自Yii,所以我想知道是否有一种方法可以在validation.php lang文件中根据模型而不是全局指定属性名。
例如,如果我有两个模型-- Employee和Company,两者都具有"name“属性,则希望将"name”属性的显示值分别设置为"Employee name“和"Company name”。
发布于 2014-09-22 14:40:12
试试看。它来自于GitHub上的文档。我从来没有与阿登特合作过,但实际上它有相当好的记录。
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类中定义的,因此它们仅适用于来自该类的输入。你也可以为公司的课程做同样的事情:
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的验证中定义了这些。但不管怎样,我希望这能帮上忙!
https://stackoverflow.com/questions/25970875
复制相似问题