更改Yii自定义表单验证程序错误消息{attribute}

更改Yii自定义表单验证程序错误消息{attribute},yii,Yii,我可以通过模型中的以下代码更改表单验证程序错误消息: array('name, email, subject, body', 'required' 'message'=>'Please enter a value for {attribute}.'), 但是我不知道{attribute}来自何处,也不知道如何更改每个字段的{attribute},因此我们非常感谢您的帮助。使用这些: return array( // name, em

我可以通过模型中的以下代码更改表单验证程序错误消息:

array('name, email, subject, body', 'required'
                        'message'=>'Please enter a value for {attribute}.'),
但是我不知道{attribute}来自何处,也不知道如何更改每个字段的{attribute},因此我们非常感谢您的帮助。

使用这些:

return array(
    // name, email, subject and body are required
    array('name', 'required',
                'message'=>'Please enter a value for name.'),
    array('email', 'required',
                'message'=>'Please enter a value for email.'),
    array('subject', 'required',
                'message'=>'Please enter a value for subject.'),
    array('body', 'required',
                'message'=>'Please enter a value for body.'),
使用这些:

return array(
    // name, email, subject and body are required
    array('name', 'required',
                'message'=>'Please enter a value for name.'),
    array('email', 'required',
                'message'=>'Please enter a value for email.'),
    array('subject', 'required',
                'message'=>'Please enter a value for subject.'),
    array('body', 'required',
                'message'=>'Please enter a value for body.'),

我不确定我是否理解你的问题,但你问{attribute}来自哪里:

一些验证器在示例中引入了{attribute}之类的占位符。如果验证失败,它们将被替换为属性名称。因此,如果没有输入名称,而您的消息是“请输入有效的{attribute}”,则错误消息将是“请输入有效的名称”

虽然{attribute}占位符可以用于每个验证器,但其中一些占位符引入了更多占位符。例如,对于,可以使用{min}、{max}或{length}。它们将分别替换为最小、最大或精确字符数

下面是一个例子:

array('firstname,lastname', 'string', 'min'=>3, 
    'tooShort'=>'Your {attribute} must contain at least {min} letters.'
),

这将给出您的名字必须包含至少3个字母。如果用户输入的字母少于3个。这样做的好处是,如果更改min参数,消息将自动更新。所以它不太容易出错。

我不确定我是否正确理解了你的问题,但你问{attribute}来自哪里:

一些验证器在示例中引入了{attribute}之类的占位符。如果验证失败,它们将被替换为属性名称。因此,如果没有输入名称,而您的消息是“请输入有效的{attribute}”,则错误消息将是“请输入有效的名称”

虽然{attribute}占位符可以用于每个验证器,但其中一些占位符引入了更多占位符。例如,对于,可以使用{min}、{max}或{length}。它们将分别替换为最小、最大或精确字符数

下面是一个例子:

array('firstname,lastname', 'string', 'min'=>3, 
    'tooShort'=>'Your {attribute} must contain at least {min} letters.'
),
这将给出您的名字必须包含至少3个字母。如果用户输入的字母少于3个。这样做的好处是,如果更改min参数,消息将自动更新。因此它不太容易出错。

从函数中获取{attribute}:

    public function attributeLabels() {
    return array(
        'id' => 'ID',
        'name' => 'Name',
        'password' => 'Password',
        'email' => 'Email',

    );
}
在您的模型上。

从函数中获取{attribute}:

    public function attributeLabels() {
    return array(
        'id' => 'ID',
        'name' => 'Name',
        'password' => 'Password',
        'email' => 'Email',

    );
}
在你的模型上