触发 Yii 字段验证 onchange 另一个字段 [英] Trigger Yii field validation onchange of another field

查看:16
本文介绍了触发 Yii 字段验证 onchange 另一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Yii 模型中有两个相关的字段.它们是 items_per 和 items_period.

I have two related fields in a Yii model. They are items_per and items_period.

items_per 是一个整数,反映在给定时间段内要处理的项目数.

items_per is an integer that reflects how many items to be processed in a given time period.

items_period 是该时间段内的秒数(带有标记为秒、分钟、小时的选项的下拉菜单).将 items_per 乘以 items_period 就得到了每秒的项目数".

items_period is the number of seconds in that period (a dropdown with options labelled as seconds, minutes, hours). Multiply items_per by items_period and you have "items per second".

我设置了一个自定义验证规则来限制每秒的项目超过一定数量.当您更改 items_per 字段中的值(模糊时)时,一切正常,并使用 ajax 验证给出合理的错误消息.

I've got a custom validation rule set up to limit items per second being above a certain amount. That all works fine and gives a sensible error message using ajax validation when you change the value in the items_per field (on blur).

我需要在 items_period 字段更改时触发对 items_per 字段的验证(可能不允许 100/秒,但允许 100/分钟).

I need for the validation on the items_per field to be triggered whenever the items_period field is changed (100 / second may not be allowed, but 100 / minute is).

我尝试在 items_per 下拉列表中添加一个 onchange 函数以触发 items_per 字段上的模糊"或更改",但它似乎没有发出 ajax 请求进行验证.提交表单只是为了触发验证不是一种选择,因为它可能没有任何错误并且只是在用户准备好之前保存记录.

I tried adding an onchange function to the items_per dropdown to trigger "blur" or "change" on the items_per field, but it doesn't seem to make the ajax request for validation. Submitting the form just to trigger the validation isn't an option, as it's possible it might not have any errors and simply save the record before the user is ready.

对于如何强制一个字段在另一个字段中触发 ajax 验证有什么建议吗?

Any suggestions how I can force one field to trigger ajax validation in another?

推荐答案

如果您定义扩展 CValidator 的自定义验证器.

You can achieve validation client-side (with JS), through AJAX and for plain requests all together in one package if you define a custom validator extending CValidator.

对于普通"验证,使用正确的属性名称和参数设置验证器并覆盖 validateAttribute 方法.

For "plain" validation, set up the validator with the correct attribute names and parameters and override the validateAttribute method.

对于客户端验证,另外覆盖clientValidateAttribute 方法.如果为表单启用了客户端验证,这将导致您的自定义 JS 自动被调用以验证输入.从覆盖中,您将输出 在此上下文中运行:

For client-side validation, additionally override the clientValidateAttribute method. If client validation is enabled for the form this will result in your custom JS automatically being called to validate the input. From within the override you will be outputting JS code that runs in this context:

function(value, messages, attribute) {
    // your code goes here
    // value: current value of attribute
    // messages: array of strings (validation errors) you should append to
    // attribute: name of the attribute
}

您可以通过 一个例子.另请参阅 CActiveForm.clientOptions.

You can see how the built-in validators work in this framework with an example. Also see CActiveForm.clientOptions.

对于 AJAX 验证,您可以提交表单进行验证.这个想法是您将验证配置为包含一个特殊参数(例如 ajax=something)或排除一个(例如不包含提交按钮的值).事实上,Yii 已经通过在所有 AJAX 验证请求中自动包含一个 ajax=formId 参数来做到这一点!

For AJAX validation, you can submit the form for validation. The idea is that you configure validation to either include a special parameter (e.g. ajax=something) or exclude one (e.g. to not include the value of your submit button). In fact, Yii already does this by automatically including an ajax=formId parameter in all AJAX validation requests!

通过这种方式,您可以轻松编写始终验证但仅在需要时保存的控制器代码.在 CActiveForm(搜索要响应 AJAX 验证请求,我们需要以下类代码:").

This way you can easily write controller code that always validates but only saves when it should. There's an example for this too in the Yii reference for CActiveForm (search for "To respond to the AJAX validation requests, we need the following class code: ").

最后,您可以通过调用 $.fn.yiiactiveform.updateInput.如果你这样做,最好通过调用 $.fn.yiiactiveform.updateSummary 来继续模仿 Yii.

Finally, you can programmatically set the validation status for any attribute with Javascript by calling $.fn.yiiactiveform.updateInput. If you do this it would be a good idea to keep imitating Yii by calling $.fn.yiiactiveform.updateSummary as well.

这篇关于触发 Yii 字段验证 onchange 另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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