【问题标题】:How to customize YII default error summary to single error如何自定义 YII 默认错误摘要为单个错误
【发布时间】:2017-03-20 14:28:03
【问题描述】:

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

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

这是我的工作集属性()。

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
    );
}

这是我的模型 rules():

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.'))
        );
    }

【问题讨论】:

  • 请发布一些代码.. 没有代码我们无法为您提供帮助
  • 看这里官方docu
  • 这个问题是针对 Yii1 还是 Yii2 的?
  • YII1 @scaisEdge
  • 更新你的问题并添加相关的模型函数规则

标签: php validation yii


【解决方案1】:

您可以在规则中添加一条消息

      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);
  }
}

并根据需要进行更改

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多