Yii Custom Form Field Id Ajax Validation CActiveForm - php
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
Related
Gravity Form Form Name Attribute
The Page Analytics I use gathers the form data automatically. Gravity Forms create tag without any name attribute. I am trying to figure out a way to assign form tag a name attribute. The closest I have reached is this example: <?php add_filter("gform_form_tag", "form_tag", 10, 2); function form_tag($form_tag, $form){ if ($form["id"] == 3){ $form_tag = preg_replace("|action='(.*?)'|", "action='custom_handler.php'", $form_tag); return $form_tag; } } ?> But I am not sure how to use preg_replace to create a name attribute for form in question.
I figured out a solution to suite myself. Sharing it for any other troubled soul. for me all I wanted was to add a name attribute for the forms so that my analytics picks up something understandable instead of ids like form-1234 so I browsed plugin/gravityforms/forms_display.php and edited it where it creates a new form tag. can be found between line 435 to 440. created a new variable to hold value of form title, edited it to remove spaces. and inserted it to the form tag string. //Edited For Analytics $cm_form_name = str_replace(" ", "-", $form['title']); $form_string .= apply_filters("gform_form_tag_{$form_id}", apply_filters("gform_form_tag", "<form method='post' enctype='multipart/form-data' {$target} id='gform_{$form_id}' name='{$cm_form_name}' {$form_css_class} action='{$action}'>", $form), $form); //End Editing //Orginal String //$form_string .= apply_filters("gform_form_tag_{$form_id}", apply_filters("gform_form_tag", "<form method='post' enctype='multipart/form-data' {$target} id='gform_{$form_id}' {$form_css_class} action='{$action}'>", $form), $form);
I wrote a plugin that also makes adding a name attribute (or modifying/adding/removing any other attribute for the form tag) a breeze. http://gravitywiz.com/gravity-forms-tag-editor/ Here's an example for adding the name attribute: new GW_Tag_Editor( array( 'tag' => 'form', 'name' => 'form_{formId}', ) );
I had a slightly different goal, but thanks to Jeff's idea I came up with the following approach that I think is worth sharing. It appends a sanitized form name data attribute to the form tag, without touching any other attributes. function add_form_name_data_attr($form_tag, $form){ $form_tag = str_replace('>', ' data-form-name="' . sanitize_title($form['title']) . '">', $form_tag); return $form_tag; } add_filter('gform_form_tag', 'add_form_name_data_attr', 10, 2); I can then use that data attribute in JavaScript to differentiate the various forms when calling a certain analytics API.
How to modify form validators from controller in Zend Framework 2?
I am working with Zend Framework 2. I have defined some validation rules within my Entity class. These work as expected without any problems. However, when in 'edit' mode I don't want the two file upload fields to be required. I tried to do setRequired(false) for both file upload fields within my controller action but it doesn't seem to have any effect on validation, it still throws a 'file was not found' message. What do I need to so the user can submit the form successfully without uploading any files? Appreciate any help.
for change form validation in controller, you must remove current validation and set new validation. i hope this help you: $form = new YourForm(); $form->setData ( $yourPostedData () ); $formInputFilter = $form->getInputFilter (); // change filter $formInputFilter->remove ( 'your_field' ); $inputFactory = new \Zend\InputFilter\Factory(); $formInputFilter->add ( $inputFactory->createInput ( array ( 'name' => 'your_field', 'required' => false ) ) ); if ($form->isValid ()) { //... }
You can use validation groups The documentation states: Zend\Form provides a proxy method to the underlying InputFilter‘s setValidationGroup() method, allowing us to perform this operation. And all you need to do is name the fields you require validating (omit the upload element names) $form->setValidationGroup(array('foo', 'bar')); if ($form->isValid()) { // $data contains just 'foo' and 'bar' $data = $form->getData(); }
The error message I was getting 'file was not found' made me realise the issue I was having was deeper than setting the validators/filters. The form is processed via an AJAX script using a FormData object. I added the files to the object using: oAgencyFormData.append("cssFilename", document.getElementById('logoName').files[0]); and the object is then passed to the AJAX call. I realised that in the case when a file is not uploaded 'undefined' is being passed to the script. So I separated it into a variable which is assigned "" if no file has been specified: var oLogoFile = document.getElementById('logoName').files[0]; var oCssFile = document.getElementById('cssFilename').files[0]; // define defaults for file upload fields (used if nothing is uploaded) if(typeof oLogoFile === 'undefined'){ oLogoFile = ''; }; if(typeof oCssFile === 'undefined'){ oCssFile = ''; }; Once I did this then file validators worked as expected.
I had the same problem and this is how I solved it: $form->remove('your_field'); $center->getInputFilter()->remove('your_field'); Hope it helps!
CakePHP 1.3: passing an argument for an action
I have a dropdown form that with option values 1, 2, and 3. When this form is submitted ($this->Form->create('MyModel', array('action' => 'view'))), I'd like one of these options to be an argument variable that gets passed into the action view and go to controller/view/option_value so that I can load some appropriate data using option_value. I can't seem to have this option_value passed as the action argument. It's a POST data, so I do have this option_value available to me in the controller, but I thought it would be good to have the value in the URL also because this view.ctp allows the user to update some things and I want the user to be redirected to the referrer easily. Any ideas? I think this is a client-side issue.. but I'm not yet familiar with Javascripts. Edit: Still looking into how to do this.. but for now, I am just redirecting the form-submitted page, which does not include the option_value in the URL, to the same page with the option_value as the parameter. It works for what I need, but if anyone would like to add an answer, I'd appreciate it!
Using jQuery: <?php echo $this->Form->create('MyModel', array('id' => 'myForm', 'action' => 'view')); echo $this->Form->input('MyField', array('id' => 'myField')); echo $this->Html->scriptStart(); ?> $('#myForm').on('submit', null, null, function(evt) { var form = $('myForm'), field = $('#myField'); form.action += "/" + field.value; form.submit(); evt.stopPropagation(); }); <?php echo $this->Html->scriptEnd(); echo $this->Form->end(__('Submit', true)); ?> Try that, you might have to tweak some stuff to your needs. Cheers. -Andrew
Yii CGridView - Custom Columns
been looking for a solution to add a feature for "Custom Columns"... Meaning, I present a list of columns that I can show the user and he selects the ones he wants to see and after the selection the table is updated and add/removes the needed columns. Didn't find anything on Google (perhaps it has a different name than what I was looking for...) Anyone has an Idea on how it can be accomplished? Thanks in advance!
This is not a complete sample, but can give you some clues on how to implement it. You've to define some kind of form to collect the data about how your grid has to be rendered. I recommend you to create a CFormModel class if there are more than 3 input fields. Create a view file with the form and a div or renderPartial of a file containing a grid: $form = $this->beginWidget('CActiveFormExt'); echo $form->errorSummary($model); echo $form->labelEx($model,'column1'); echo $form->dropDownList($model echo $form->error($model,'column1'); echo CHtml::ajaxSubmitButton('UpdateGrid',array('controller/grid'), array('update'=>'#grid'), $this->endWidget(); // you can render the 'default options' before any ajax update $this->renderPartial('_grid',array($customColumns=>array('id','name'),'dataProvider'=>$dataProvider)); In the _grid.php view file: $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'grid', 'dataProvider'=>$dataProvider, 'columns' => $customColumns; )); In the controller: function actionGrid(){ // recover the form data, and build the custom columns array $customColumns = array(); $customColumns[] = '.....'; $dataProvider = ...; $this->renderPartial('_formTrabajo', array('customColumns' => $idSiniestro, 'dataProvider' => $dataProvider'), false); } When you click the ajaxSubmitButton, the form is sent to the url specified through ajax, and the reply from the controller must contain the renderPartial of the view containing the grid, so the jQuery call can replace the html correctly. You must pass an array from your controller to the partial view of the grid, with the custom list of columns you want to display.
Dynamic form fields in Symfony 1.4
I'm working on an e-commerce project and I got stuck at the cart update. Here I have to present a form using the contents of the current cart, with input fields containing the current quantities. I checked the documentation and the forums, but I didn't find anything useful. The problem is that i cannot declare the exact form fields in my form class because I don't know how many fields will be there. I tried this: class CartForm extends sfForm { public function configure() { $cart = sfContext::getInstance()->getUser()->getShoppingCart(); foreach ($cart->getItems() as $item) { $widgetName = $item->getId().'_quantity'; $this->widgetSchema[$widgetName] = new sfWidgetFormInput( array(), array( 'class' => 'quantity-input', 'id' => null, 'name' => $widgetName ) ); $this->widgetSchema->setDefault($widgetName, $item->getQuantity()); $this->validatorSchema[$widgetName] = new sfValidatorInteger(array( 'required' => true, 'min' => 1 ), array()); } unset($cart); $this->getWidgetSchema()->getFormFormatter()->setRowFormat('%field%%error%%hidden_fields%'); } } but I got some errors: Fatal error: Cannot use object of type sfShoppingCart as array in /home/sfprojects/mdmall/lib/vendor/symfony/lib/form/sfForm.class.php on line 784 so this is not the right way. I tried to use raw fields without any form classes (and validators) but something very odd happens, instead of getting the $_POST values i get a 404 error because when I submit the form it doesn't trigger this: cart_update: url: /cart/update.:sf_format class: sfRequestRoute param: { module: cart, action: update, sf_format: html } requirements: { sf_method: post } If I remove the requirement, cart/update runs, but I dont have the $_POST data in the request object. Do you have any ideas?
These will help you with regard to dynamically adding form fields and working with validation of those fields: Dynamic Emedded forms (but the process is similar for fields) Dynamic Form Fields Custom Validation