dongmibeng5885 2012-03-30 04:11
浏览 24

Yii的CActiveForm错误消息不会消失

I'm writing a couple of (custom) validators for my form and everything's going smoothly as always but for one little issue.

I have two fields: one is a html select input (aka dropDownList) and the other is a simple textField.

I've setup my ajax validators so that, when option X is selected on my dropDownList, the text in the textField can't be empty.

SO when a user selects option X in the dropDownList, an error message (for the dropDownList, mind you) comes up: text field Y can't be empty.

Good so far. When I then click on the textField, type some stuff and click somewhere else on the screen, sure enough the ajax validator is fired off, and the HTTP response is empty meaning there are no errors on the form. But the error message does not disappear!

I'm guessing this has something to do with the fact that I'm using one field's validator to clearErrors() on another field, but shouldn't this work? I've given the correct field as parameter to the clearErrors() method..

Has anyone any idea of what might be going wrong for me?

Thanks in advance

EDIT here's the link to my Form Model: click

when I change target_constant or source_constant, a validator is fired off to the server. It is working, and by that I mean that an AJAX response does get sent by the server to my validator being fired off. In addition, the response body has no error messages, but still the error message for target_reg or source_reg (depending upon which of those was affected) does not go away in the same instant. Only in the next request/response does the error message disappear.

  • 写回答

2条回答 默认 最新

  • douyu0845 2012-05-12 18:52
    关注

    I just had a look at this, but couldn't find something wrong with your code. It seems that the actual validation works (you get the right data back), but the Javascript (yiiactiveform.js) only updates the data for the field last edited. This is contrary to a server-side validation, which checks all fields. From a user point of view, it makes sense to only display the error for the field you are currently editing, but when you have complex interactions, this is of course a little lacking. I was procrastinating heavily here, so I came up with a "solution". You can use the afterValidateAttribute callback on any attribute to force rendering error messages with $.fn.yiiactiveform.updateInput:

        <div class="row">
        <?php echo $form->labelEx($model,'text'); ?>
        <?php echo $form->textField($model,'text'); ?>
        <?php echo $form->error($model,'text', array('afterValidateAttribute' => 'js:function(form, attribute, data, hasError)
        { 
            mSettings = $.fn.yiiactiveform.getSettings(form);
            $.fn.yiiactiveform.updateInput(mSettings.attributes[0], data, form)
        }')); ?>
    </div>
    

    What does it do? in the callback, I get to work with some data. Most interesting here is data - this is the AJAX JSON response with errors for all attributes. In the function I then get the settings for the currentform. This is important, because I have to find the attribute object I want to manipulate. I then call updateInput manually, and pass the attribute object I want to modify, as well as the data and the form from the callback. Problem is here that you have to find out which zero-based index in the attributes object is your object. I did this by console.log. Et voila, your attribute gets updated. I just hope there isn't an easier way to do this, and I spend this much time on it for nothing. :p

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看