I'm using Zend_Form and have got stuck in a situation. I have an array of some values like below :
$feeTypes = array(1,2,3,4);
and want to create 6 text elements in zend form that should be rendered like following:
<input type="text" name="class_fee_type[1]" />
<input type="text" name="class_fee_type[2]" />
<input type="text" name="class_fee_type[3]" />
<input type="text" name="class_fee_type[4]" />
I tried the following code but don't know that how to associate the above $feeType array.
$class_fee_type = $this->CreateElement('text','class_fee_type')
->setRequired(false)
->setAllowEmpty(false)
->setIsArray(true)
->setRegisterInArrayValidator(false)
->setDecorators(array( array('ViewHelper')
));
I'm not using any subform in this form. This is absolutely simple zend form.
Thanks.
I'm talking from ZF1 point of view:
It seems like there is no standard way of doing what you need. So I would recommend write your own form element and element view helper for that. And passing your indexes array as options to element.
Note: to use setRegisterInArrayValidator - your Zend form element should extend Zend_Form_Element_Multi.
Related
As title says, how can i generate a form using zend form having input inside label?
Something like
<label class="checkbox" for="persistent">
<input type="checkbox" value="" id="persistent" data-toggle="checkbox">
Remember Me
</label>
Thanks!
You can likely add a decorator to your input field.
I have multiple checkboxes in zend ,I want that when i check them they are inserted into database .how to use that as an array and use for each to break them .I want to do this in my model insert them in in database.
You can define checkbox names as array:
<form action="" method="post">
<input type="checkbox" name="check[]" value="1" /> option one
<input type="checkbox" name="check[]" value="2" /> option two
<input type="checkbox" name="check[]" value="3" /> option three
</form>
then, when you submit form, you should get your $_POST[check] variable filled in with ids of checked options. You can then foreach through this to insert into your table.
You can get the array on your controller.
$chkArr=$this->_getParam('chkboxName');
After getting this you can pass this array to your model.
You can check the count etc. in model or controller depends on your requirement.
I would like to do multiple rows selection. Rows are display through strut2 tag s:iterator, how can I get the selection information, which should contains a list of selected "id"
<s:form action='Selection'>
<s:iterator value="transInfos">
<input type='hidden' name=id value='<s:property value="id" />' />
<s:checkbox name="selected"/>
<s:property value="name" />
</s:iterator>
<s:submit value="Selection" />
</s:form>
One option which seems to me is to create a hidden field in your form like
<s:form action="selection">
<input type='hidden' name="selectedId" value=""/>
</s:form>
you can add a on-click event to your check-box and if it get checked you can add the value t a variable and set in the hidden field,each new addition should be added as new values in comma separated way like in end hidden field should be like
<input type='hidden' name="selectedId" value="1,2,3,4"/>
moment you submit the form you can parse the form value and can split it based on the separator ","
other option is to name the check-box with same name so moment it will get submitted the values of the checked one will be submitted as a collection, choice is all yours and you need to decide which way to go
I'm glad I can answer this question myself.
The answer is quite simple.
<s:form action="..." >
<s:iterator value="transInfos">
<input type="checkbox" name="transIds" value='<s:property value="transID" />'/>
</s:iterator>
<s:submit value="Select"/>
</s:form>
the value of checkbox is what you want to pass to the action, all the selected checkboxes will pass their values as a list to the action.
I have a 'customer' form which has a section called 'contacts'. To start with this contacts section will contain the following elements..
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
But the user may want to add another contact which will duplicate the elements with javascript to produce the following:
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
<br />
<input type="text" name="contacts[1][fname]" />
<input type="text" name="contacts[1][sname]" />
I know how to produce the first set of elements, however if the form gets submitted and there are errors, how can i ensure that the correct number of 'contacts' elements get rendered?
Ive never had to do this with Zend_Form but i have done it with Symfony 1.4's sfForm which has a similar API and theory of operation. Based on that the basic process is:
In the parent forms constructor intialize some default number of subforms. Youll want to separate out the logic for actually creating and embedding n subforms into a separate method(s). Ill refer to this as the method emebedContacts($count = 1)
Override the isValid and setDefaults methods on the parent form so that they detect the number of subforms in the $data arguments passed to them and then call embedContacts before calling parent::isValid() or parent::setDefaults().
Hope that helps.
I have some custom validation I'd like to do with jQuery. jQuery validation comes out of the box with a 'required' class. This means that I can have a script declaring
$('form').validate()
and the next textbox will validate correctly, based on the class 'required' I add to it:
<input type="text" value="" name="flowstatus" id="flowstatus" class="required">
My new validation rule is: the text in a textbox should be between 15 and 50 chars long. Is there a way to create my own custom rule/class to implement something like
<input type="text" value="" name="flowstatus" id="flowstatus" class="from15to50">
Note: This is NOT what I need:
('form').validate({
rules: {
flowstatus: {required: true, minlength: 15, maxlength: 50}
}});
This last script is too tightly coupled to the fields I want to validate.
I'm pretty sure that you can add an attribute to the desired element defining its min and max lengths. Something like:
<input type="text" value="" name="flowstatus" id="flowstatus" class="required" minlength="15" maxlength="50">
I'm not quite sure about the custom class solution, however.