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

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

问题描述

我在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_period乘以items_per并且你有每秒项目数。

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_per上进行验证每当items_period字段被更改时触发的字段(可能不允许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?

推荐答案

你可以实现验证客户端(使用JS) ,如果您定义了一个自定义验证器,可以通过AJAX 一起在一个包中进行简单请求 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 =某些)或排除一个(例如,不包括提交按钮的值)。 事实上,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: ").

最后,您可以通过编程方式使用Javascript通过调用 $。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字段验证另一个字段的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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