Testing a Yii trigger on another field

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

items_per is an integer that reflects the number of items that will be processed over a given period of time.

items_period is the number of seconds in this period (a drop-down list with parameters indicated as seconds, minutes, hours). Multiply items_per by items_period and you have "items per second".

I have a custom validation rule configured to limit the number of items per second above a certain amount. All this works fine and gives a reasonable error message using the ajax check when changing the value in the items_per field (when blurring).

I need the check in the items_per field to run whenever the items_period field changes (100 / second is not allowed, but 100 / minute).

I tried adding the onchange function to the items_per drop-down list to cause a “blur” or “change” to the items_per field, but it does not seem to make an ajax request for verification. Submitting the form only to start validation is not an option, since it is possible, it may not have any errors and just save the record before the user is ready.

Any suggestions how can I get one field to activate ajax check in another?

+4
source share
2 answers

You can achieve client-side validation (using JS), through AJAX, and for simple queries together in one package, if you define a custom validator that extends CValidator .

For a “simple” check, install a validator with the correct attribute names and parameters and override the validateAttribute method.

For client-side validation, additionally override the clientValidateAttribute method. If client validation is enabled for the form, this will cause your custom JS to be automatically called to validate the input. Inside the override, you will output JS code that works 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 } 

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

To verify AJAX, you can send a confirmation form. The idea is that you configure the check to either enable a special parameter (for example, ajax=something ), or to exclude it (for example, to not include the value of the submit button). In fact, Yii already does this by automatically including the ajax=formId in all AJAX validation requests!

Thus, you can easily write controller code that always checks, but saves only when necessary. Here is an example of this in the Yii link for CActiveForm (search "To answer AJAX validation requests, we need the following code class:").

Finally, you can programmatically set the validation status for any attribute using Javascript by calling $.fn.yiiactiveform.updateInput . If you do this, it would be nice to save the Yii simulation by calling $.fn.yiiactiveform.updateSummary as well.

+4
source

I created the following javascript function, which I manually call when changing certain fields. It performs an ajax check for all form elements.

  / **
  * @author marcovtwout
  * Force performing full ajax validation on given form.
  * @param Object $ form jQuery form object
  * /
 performFullAjaxValidation = function ($ form) {
     var settings = $ form.data ("settings");
     $ .each (settings.attributes, function () {
         this.status = 2;  // force ajax validation
     });
     $ form.data ("settings", settings);

     // trigger ajax validation
     $ .fn.yiiactiveform.validate ($ form, function (data) {
         $ .each (settings.attributes, function () {
             $ .fn.yiiactiveform.updateInput (this, data, $ form);
         });
         $ .fn.yiiactiveform.updateSummary ($ form, data);
     });
 }
+5
source

All Articles