【问题标题】:Yii2 render html content in custom validation rule addError methodYii2 在自定义验证规则 addError 方法中渲染 html 内容
【发布时间】:2015-04-12 17:08:44
【问题描述】:

我已经定义了自定义验证规则:

['Email', 'checkUniqueExistence'],

public function checkUniqueExistence($attribute, $params)
{
        $email = $this->$attribute;

        $checkUniqueExistenceForEmail = Parents::find()->where([ 'Email' =>  $email])->One();

        $resendActivationLink = Html::a('Resend Link','#');

        if(count($checkUniqueExistenceForEmail) > 0){
            if($checkUniqueExistenceForEmail->IsAccountActivated == Yii::$app->params['IsAccountActivatedTrue']){
                $this->addError($attribute, 'Email address already exists');
            }
            else{
                $this->addError($attribute, 'You haven\'t activated your account. Request '.$resendActivationLink);
            }
        }

}

但链接不会生成。它显示为字符串。提交表单后得到以下输出:

You haven't activated your account. Request <a href="#">Resend Link</a>

是否可以在 addError() 方法中显示链接?

【问题讨论】:

  • 一个更有效的方法是根据错误对象是否设置有条件地显示重新发送链接..
  • eg : if($this->hasError) { show_link } else { hide_link } ,您可以在视图中添加类似的内容
  • 谢谢,我会试一试。但是问题还是出现了,为什么 addError 方法不渲染 HTML?

标签: model yii2 yii2-advanced-app


【解决方案1】:

将此配置数组传递给 ActiveField

'errorOptions' => ['encode' => false]

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2014-07-29
    相关资源
    最近更新 更多