I'm making a system that stores Yii Forms in a database.
I'm having troubles getting one thing to work.
I have several Forms on the same page which are all called DynamicFormModel. To achieve validation on each field I had to use the 'id' and 'inputID' HtmlOptions on my fields and error message dom elements.
$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));
Client side validation works fine. The problem I'm having is with Ajax Validation. The client side validation now makes the needed events relative to the needed field 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});
});
/*]]>*/
The Ajax Validation does not return with the ID of the field but the attribute name. This is the response I'm getting:
{"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."]}
The binding that is trying to be made will not work. The reason is that I get:
"DynamicFormModel_test":["Test cannot be blank."]
Whereas I need it to bind to for example: form_2language_6field_4_em_
Suggestions please!
Cause was due to a bug. See bugfix https://github.com/yiisoft/yii/issues/3144
I ended up using a session array and storing data there so that I can reference it again later. I just added my post data from each form into this array and referenced it later in my else block. Thanks for the help!
I am using CodeIgniter for a school project. I have some experience with PHP but am relatively new to this framework. I am having trouble using two forms on one page.
I have one form that displays a dropdown of artists. After clicking the submit button for this form, it updates the second form (another dropdown) on the same page with the portfolios belonging to the artist selected in the first dropdown.
I am trying to echo the values from each form just for testing purposes right now, I will implement other features later. My issue is that after my second form is submitted, the post value for the first form is changed. If I echo the selected value of the first form before submitting the second form, it shows the value that was selected. If I echo the value of the first form after both forms have been submitted, it shows the first available value from that dropdown.
I need to be able to take the values from both of these forms and then use them later after both forms have been submitted. So I can't have the values changing right when I need to use them, obviously, any help would be appreciated. Thank you much.
Controller
public function formtest(){
//Making a call to the model to get an array of artists from the DB
$data = $this->depot_model->get_artists_list();
$this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
$this->form_validation->set_rules('ports', 'Portfolios', 'required');
if ($this->form_validation->run() == FALSE)
{
//Building the artists dropdown form
$data['data'] = form_open('formtest', 'class="superform"')
. form_label('Artist<br/>', 'artist')
. form_dropdown('artist', $data)
. form_submit('mysubmit', 'Submit')
. form_close();
//Setting up a temp array of the selected artist's portfolios
$ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));
//Culling out the names of the portfolios from my temp array
$newdata = array();
foreach($ports as $port){array_push($newdata, $port['name']);}
//Building the artist's portfolio dropdown
$newdata['data'] = form_open('formtest', 'class="superform"')
. form_label('Portfolios<br/>', 'ports')
. form_dropdown('ports', $newdata)
. form_submit('mysubmit', 'Submit')
. form_close();
//Send the information to my view
$this->load->view('formtest', $data);
$this->load->view('formtest', $newdata);
}
else
{
//This echos out the first available value from my dropdown rather than the one I selected.
echo $data[$this->input->post('artist')];
echo "success";
}
}
The forms are separate, so when the second gets submitted, there is in effect no value received from the first form, as it isn't included as a field in the second. So you can do that, include say a hidden field in the second form that has the value of the artist. eg:
$newdata['data'] = form_open(
'formtest',
'class="superform"',
array('artist' => $this->input->post('artist'))
);