问题描述
限时送ChatGPT账号..如何在锚标签标题中放置yii2表单错误
这是我的代码
$form = ActiveForm::begin(['id' =>'登录表格','选项' =>['类' =>'形式水平'],'fieldConfig' =>['模板' =>'{标签}<div class="error-block"><a href="#" title="{error}">error</a>{输入}','错误选项' =>['标签' =>空值]],]);
我想在 YII2 中的锚标签标题中添加错误
error</a>
解决方案 你需要在锚标签 title
属性内显示错误文本,并使用 template
选项不会自动帮你实现.
你的方法中的一些事情.
您在 errorOptions
下指定了 "tag"=>null
这不会创建默认的 html <p class="help-block help-block-error"></p>
.
即使你将标签指定为 div
或 span
即使它会破坏 HTML,因为属性标题有双引号 <ahref="#";title="{error}">error</a>
并且创建的标签也将带有双引号
.
那么如果你改变了 到
修复损坏的
HTML
javascript 将无法检测到该元素.
那么事件 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
离开字段或点击提交按钮,您将看到错误消息填充在a的
title
属性中代码>标签.
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 toattributeDefaults
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屋!
更多推荐
[db:关键词]
发布评论