我正在建立一个将Yii Forms存储在数据库中的系统。
我有麻烦让一件事情起作用。
我在同一页面上有几个表单,它们都叫做DynamicFormModel。要在每个字段上实现验证,我必须在我的字段和错误消息dom元素上使用'id'和'inputID'HtmlOptions。
$domId = 'form_' . $dynamicFormModel->formModel->id . 'language_' . $dynamicFormModel->language->id . 'field_' . $formField->id;
echo $form->textArea($dynamicFormModel, $formField->label, array('id' => $domId));
echo $form->error($dynamicFormModel, $formField->label, array('inputID' => $domId));
客户端验证工作正常。我遇到的问题是Ajax验证。客户端验证现在会根据所需的字段ID生成所需的事件。
/*<![CDATA[*/
jQuery(function($) {
jQuery('#AppVersionDetailsForm').yiiactiveform({'attributes':[{'id':'AppVersionDetailsForm_defaultLanguage','inputID':'AppVersionDetailsForm_defaultLanguage','errorID':'AppVersionDetailsForm_defaultLanguage_em_','model':'AppVersionDetailsForm','name':'AppVersionDetailsForm[defaultLanguage]','enableAjaxValidation':true,'inputContainer':'div.control-group','summary':true},{'id':'AppVersionDetailsForm_selectedLanguages','inputID':'AppVersionDetailsForm_selectedLanguages','errorID':'AppVersionDetailsForm_selectedLanguages_em_','model':'AppVersionDetailsForm','name':'AppVersionDetailsForm[selectedLanguages]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'summaryID':'AppVersionDetailsForm_es_','errorCss':'error'});
jQuery('#form_1language_1').yiiactiveform({'validateOnType':true,'validateOnChange':true,'attributes':[{'id':'DynamicFormModel_Test','inputID':'form_1language_1field_1','errorID':'form_1language_1field_1_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':false,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Radio','inputID':'form_1language_1field_2','errorID':'form_1language_1field_2_em_','model':'DynamicFormModel','name':'DynamicFormModel[Radio]','enableAjaxValidation':false,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_1').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_1field_3','errorID':'form_2language_1field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_1field_4','errorID':'form_2language_1field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_1field_5','errorID':'form_2language_1field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_1field_6','errorID':'form_2language_1field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_1field_7','errorID':'form_2language_1field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_1field_8','errorID':'form_2language_1field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_3').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_3field_3','errorID':'form_2language_3field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_3field_4','errorID':'form_2language_3field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_3field_5','errorID':'form_2language_3field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_3field_6','errorID':'form_2language_3field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_3field_7','errorID':'form_2language_3field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_3field_8','errorID':'form_2language_3field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_4').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_4field_3','errorID':'form_2language_4field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_4field_4','errorID':'form_2language_4field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_4field_5','errorID':'form_2language_4field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_4field_6','errorID':'form_2language_4field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_4field_7','errorID':'form_2language_4field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_4field_8','errorID':'form_2language_4field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_6').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_6field_3','errorID':'form_2language_6field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_6field_4','errorID':'form_2language_6field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_6field_5','errorID':'form_2language_6field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_6field_6','errorID':'form_2language_6field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_6field_7','errorID':'form_2language_6field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_6field_8','errorID':'form_2language_6field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('body').tooltip({'selector':'a[rel=tooltip]'});
jQuery('body').popover({'selector':'a[rel=popover]'});
jQuery('#yii_bootstrap_collapse_0').collapse({'parent':false,'toggle':false});
});
/*]]>*/
Ajax Validation不返回字段的ID,而是返回属性名称。这是我得到的回应:
{"DynamicFormModel_test":["Test cannot be blank."],"DynamicFormModel_radio":["Radio cannot be blank."],"DynamicFormModel_Check":["Check cannot be blank."],"DynamicFormModel_this":["This cannot be blank."],"DynamicFormModel_Upload":["Upload cannot be blank.","Upload cannot be blank."],"DynamicFormModel_a":["A cannot be blank."],"DynamicFormModel_file":["File cannot be blank."],"DynamicFormModel_an":["An cannot be blank."],"DynamicFormModel_image":["Image cannot be blank."]}
尝试进行的绑定不起作用。原因是我得到了:
"DynamicFormModel_test":["Test cannot be blank."]
然而我需要它绑定到例如:form_2language_6field_4_em_
建议请!