如何在锚标签的标题中放置yii2表单错误 [英] how to place yii2 form error in title of anchor tag

查看:21
本文介绍了如何在锚标签的标题中放置yii2表单错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在锚标签标题中放置yii2表单错误

这是我的代码

$form = ActiveForm::begin(['id' =>'登录表格','选项' =>['类' =>'形式水平'],'fieldConfig' =>['模板' =>'{标签}<div class="error-block"><a href="#" title="{error}">error</a>

{输入}','错误选项' =>['标签' =>空值]],]);

我想在 YII2 中的锚标签标题中添加错误

error</a>

你需要在锚标签 title 属性内显示错误文本,并使用 template 选项不会自动帮你实现.

你的方法中的一些事情.

那么事件 afterValidateAttribute 就会派上用场,它会在验证整个表单和每个属性后触发.

<块引用>

事件处理程序的签名应该是:

函数(事件、属性、消息)

哪里

  • event:一个事件对象.

  • attribute:被验证的属性.该参数的结构请参考attributeDefaults.

  • messages:一个数组,您可以向其中添加指定属性的其他验证错误消息.

现在将 fieldConfig 下的 template 更改为以下内容,并从模板中删除 {error}errorOptions,因为它不是必需的,只需将自定义错误元素保留在模板中即可.

'fieldConfig' =>['模板' =>'{标签}<div class="error-block"><a href="#";标题=">错误</a>

{输入}',],

现在在您渲染表单的视图顶部添加以下 javascript.

$js = <<<JS$("#login-form").on('afterValidateAttribute',功能(事件,属性,消息){让 input=attribute.input;$(input).siblings('.error-block').find('a').attr('title',messages.join(','));});JS;$this->registerJs($js, \yii\web\View::POS_READY);

现在,如果您focus离开字段或点击提交按钮,您将看到错误消息填充在atitle属性中代码>标签.

how to place yii2 form error in title of anchor tag

This is my code

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => '
            {label}
            <div class="error-block">
                <a href="#" title="{error}">error</a>
            </div>
            {input}
            ',
        'errorOptions' => ['tag' => null]
    ],
]);

I want to add error in title of anchor tag in YII2

<a href="#" title="{error}">error</a>

解决方案

You need to display the error text inside the anchor tags title attribute, and using the template option wont help you achieve it automatically.

A few things in your approach.

  • You are specifying the "tag"=>null under the errorOptions which wont create the default html <p class="help-block help-block-error"></p>.

  • Even if you specify the tag as div or span even then it will break the HTML as the attribute title has double quotes <a href="#" title="{error}">error</a> and the tag created will also have double quotes <div class="help-block help-block-error"></div>.

  • Then if you change the <a href="#" title="{error}">error</a> to <a href="#" title=\'{error}\'>error</a> to fix the broken HTML the javascript wont be able to detect the element.

So then the event afterValidateAttribute will come to your rescue which is triggered after validating the whole form and each attribute.

The signature of the event handler should be:

function (event, attribute, messages)

where

  • event: an Event object.

  • attribute: the attribute being validated. Please refer to attributeDefaults for the structure of this parameter.

  • messages: an array to which you can add additional validation error messages for the specified attribute.

Now change the template under the fieldConfig to the following and remove the {error} from the template and the errorOptions, as it is not needed and just keep you custom error element inside the template.

'fieldConfig' => [
    'template' => '
        {label}
        <div class="error-block">
            <a href="#" title="">error</a>
        </div>
        {input}
        ',
],

Now add the below javascript at the top of your view where you are rendering the form.

$js = <<< JS
$("#login-form").on(
    'afterValidateAttribute',
    function (event,attribute,messages) {
        let input=attribute.input;
        $(input).siblings('.error-block').find('a').attr('title',messages.join(','));
    }
);
JS;
$this->registerJs($js, \yii\web\View::POS_READY);

Now if you focus out of the fields or hit the submit button you will see the error messages being populated in the title attribute of the a tag.

这篇关于如何在锚标签的标题中放置yii2表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆