如何将YII默认错误摘要自定义为单个错误

时间:2016-11-07 09:35:28

标签: php validation yii

需要更改默认的YII错误摘要,

enter image description here

在特定表单字段中显示每个错误,如下图所示。

enter image description here

这是我的worklet属性()。

public function properties() {
    return array(
        'action' => url('/user/signup', array('dialog' => isset($_GET['dialog']) ? $_GET['dialog'] : 0)),
        'elements' => array(
            'firstName' => array('type' => 'text'),
            'lastName' => array('type' => 'text'),
            'email' => array('type' => 'text'),
            'password' => array('type' => 'password'),
            'passwordRepeat' => array('type' => 'password'),
            'profile' => wm()->get('user.profile.helper')->form(),
            '<hr />',
            'termsAgree' => array(
                'type' => 'checkbox',
                'layout' => "<fieldset>{input}\n{label}\n{hint}\n{error}</fieldset>",
                'uncheckValue' => '',
                'required' => false,
                'afterLabel' => '',
            ),
        ),
        'buttons' => array(
            'submit' => array('type' => 'submit',
                'id'=>'signUpButton',
                'label' => $this->t('Sign Up')),
        ),

        'model' => $this->model
    );
}

以下是该模型的规则():

public function rules()
    {
        return array(
            array('email, password, passwordRepeat, firstName, lastName', 'required'),
            array('firstName, lastName, email', 'length', 'max' => 250),
            array('email', 'email'),
            array('email', 'unique', 'className' => 'MUser', 'message' => $this->t('This email is already in use.')),
            array('password', 'length', 'min' => 6),
            array('passwordRepeat', 'compare', 'compareAttribute'=>'password'),
            array('termsAgree', 'required', 'message' => $this->t('You must be agree to our Terms of Use and Privacy Policy to join this site.'))
        );
    }

1 个答案:

答案 0 :(得分:0)

您可以在规则

中添加消息
      array('email, password, passwordRepeat, firstName, lastName', 'required',
              'message'=>'{attribute} cannot be blank, please enter a value .'),

在你的控制器中寻找

public function actionError()
{
  if($error=Yii::app()->errorHandler->error)
  {           
    if(Yii::app()->request->isAjaxRequest) {
    echo $error['message'];
  }
  else
    $this->render('error', $error);
  }
}

并根据需要进行更改