Add many select elements with [] in name - php

I want to be able to add to Zend_Form many Zend_Form_Element_Select.
I've got some loop in My_Form_Selects extends Zend_Form with
$element = $this->createElement('Select', 'element[]');
$this->addElement($element);
but it creates only one select element (Zend_Form ignores [] in element's name).
How should I do this ?

Have you tried:
$foo = new Zend_Form_SubForm();
$foo->setElementsBelongTo('foo')
->setElements(array(
'bar' => 'text',
'baz' => 'text'
));
echo $foo;
which results in HTML something like:
<input type="text" name="foo[bar]" id="foo.bar" value="" />
<input type="text" name="foo[baz]" id="foo.baz" value="" />
via
From the manual:
Zend_Form::setIsArray($flag): By setting the flag TRUE, you can indicate that an entire form should be treated as an array. By default, the form's name will be used as the name of the array, unless setElementsBelongTo() has been called. If the form has no specified name, or if setElementsBelongTo() has not been set, this flag will be ignored (as there is no array name to which the elements may belong).
You may determine if a form is being treated as an array using the isArray() accessor.
Zend_Form::setElementsBelongTo($array): Using this method, you can specify the name of an array to which all elements of the form belong. You can determine the name using the getElementsBelongTo() accessor.

Related

How to pass associative array to a php page in POST?

I am trying to use Magento 1.9 XmlConnect module to save the billing address function. In xml connect there is an action to do this, saveBillingAddressAction. In savebillingaddressAction method one line is trying to access an array from the POST variables like following -
$data = $this->getRequest()->getPost('billing', array());
How I can pass an array from client side to server side in POST variable so that billing param have the array with the needed data?
Magento repository - CheckoutController.php.
You can create arrays out of form elements using square brackets [].
<input type="hidden" name="billing[]" value="billing-info1">
<input type="hidden" name="billing[]" value="billing-info2">
<input type="hidden" name="billing[]" value="billing-info3">
This will return a zero based array (ie 0=>'billing-info1',1=>'billing-info2', etc).
If you'd like to use an associative array you just need to create a key:
<input type="hidden" name="billing[key0]" value="billing-info1">
<input type="hidden" name="billing[key1]" value="billing-info2">
<input type="hidden" name="billing[key2]" value="billing-info3">
Then your return will be something like:
'key0' => 'billing-info1',
'key1' => 'billing-info2',
'key2' => 'billing-info3'

Yii - cannot define a safe array of attributes for form submit

I have a PHP form that need to submit a array of numbers, what we have in view:
<input type="text" id="ProductForm_sizeobj_1" name="ProductForm[sizeobj[1]]" value="13">
<input type="text" id="ProductForm_sizeobj_2" name="ProductForm[sizeobj[2]]" value="13">
<input type="text" id="ProductForm_sizeobj_3" name="ProductForm[sizeobj[3]]" value="13">
And I define in form class:
public $sizeobj = array();
public function rules() {
return array(
array('/** other attributes **/, sizeobj', 'safe')
);
}
Since "Sizeobj" is a dynamic attribute and the size will growth more then 3, therefore I use array. However after form submitted the error throw as follow:
Failed to set unsafe attribute "sizeobj[1" of "ProductForm".
I believe I might using the wrong method to setup array attribute, or wrong rule, any advice? I'm new to Yii, any help is appreciated.
Use name="ProductForm[sizeobj][1]" instead of name="ProductForm[sizeobj[1]]"

Checkbox checked using Zend Form

IN this way checkbox is created
$is_conveyance_required = new Zend_Form_Element_Checkbox(FORM_CHECKBOX_PREFIX . 'is_conveyance_required', array());
$is_conveyance_required->addDecorators(array(
array('HtmlTag', array('tag' => 'label')),
array('Label', array('tag' => '')),
));
$is_conveyance_required->setValue(1);
$is_conveyance_required->setChecked( true );
$this->addElement($is_conveyance_required);
and how form populating
$personal_form->populate($personal_data);
But zend form not populating checkbox...
<input type="checkbox" value="1" id="chk_is_conveyance_required" name="chk_is_conveyance_required">
Here is $personal_data array img
It's simply not working how you expect it to operate.
From the Zend Manual:
By default, the checked value is '1', and the unchecked value '0'. You
can specify the values to use using the setCheckedValue() and
setUncheckedValue() accessors, respectively. Additionally, setting the
value sets the checked property of the checkbox. You can query this
using isChecked() or simply accessing the property. Using the
setChecked($flag) method will both set the state of the flag as well
as set the appropriate checked or unchecked value in the element.
Please use this method when setting the checked state of a checkbox
element to ensure the value is set properly.
You could do some workaround by using something like this (untested!) in you controller as addition to popuplate()
if($personal_form->is_conveyance_required->getValue() == 1) {
$personal_form->is_conveyance_required->setChecked(true);
}

zend framework text fields with same name but with different belongings

I have a problem with zend form, where i need to built a form where field name are same but with different belongings. Here is the input fields that i wanted inside my form.
Currently i am getting with straight html but because of this i am missing validation.
<input type="text" name="travel_guide_tab[4][title]">
<input type="text" name="travel_guide_tab[4][description]">
<input type="text" name="travel_guide_tab[6][title]">
<input type="text" name="travel_guide_tab[6][description]">
In Zend Form the element names must be unique (in some way) or else they will overwrite. However you can continue using your html form and simply filter and validate in the controller using Zend_Filter_Input. The filter and validation classes are the same ones used by Zend_Form you just pass the data in a different way.
Simple example, partial:
public function someAction() {
//set filters and validators for Zend_Filter_Input
$filters = array(
'nameOfInput' => array('HtmlEntities', 'StripTags')
);
$validators = array(
'nameOfInput' => array('NotEmpty', 'Int')
);
//assign Input
$input = new Zend_Filter_Input($filters, $validators);//can also pass the data in constructor (optional)
$input->setData($this->getRequest()->getParams());
//check input is valid and is specifically posted as 'Delete Selected'
if ($input->isValid()) {
//do some stuff
}
Good Luck.

Creating Link to Same Page with Request Parameters in Symfony

How might one use the url_for or link_to helpers to create a link to the same page passing along all GET parameters?
I have a search page which accepts multiple parameters as filters to hide certain results. For example, search.html?query=abc&people=1&groups=0 would search for all items containing 'abc' that aren't groups. I would therefore like a link which toggles these filters, passing the rest of the parameters unchanged to the current page. In the above example, links would be created to search.html?query=abc&people=1&groups=1 and search.html?query=abc&people=0&groups=0.
I am wondering if there is any way to specify 'this' as a route, such that if the route or page changed, the code wouldn't have to.
Moreover, how can one pass all parameters to the helper? $sf_params can be used to access all the parameters, and the 'query_string' property can be passed to the helper, but is there any method of combining the two short of creating the string manually?
GET parameters can be passed to a link_to method as an array in the third argument, or url_for in the second. This is illustrated as follows:
link_to('Text to display', 'routename', array('group' => 0, 'people' => 1), $link_attributes);
url_for('routename', array('group' => 0, 'people' => 1));
This is hidden in the source code. Here, $params is the list of GET parameters, and $options HTML attributes to add to the tag.
function link_to2($name, $routeName, $params, $options = array())
function url_for2($routeName, $params = array(), $absolute = false)
The $sf_params function can be converted to a suitable array for passing with getAll(). See the documentation here. Note that this is different from previous versions of Symfony. Also be aware you might have to call getRawValue() from within a template:
$params = $sf_params->getRawValue()->getAll();
$sf_context->getInstance()->getRouting()->getCurrentRouteName() can then be passed as $routeName to get the current route. Thanks to Tom for this
put vars in hidden form data types to pass them on
<form action="page.php" method="GET">
<input name="people" type="hidden" value="<?php echo htmlspecialchars($_GET['people'], ENT_QUOTES); ?>">
<input name="groups" type="hidden" value="<?php echo htmlspecialchars($_GET['groups'], ENT_QUOTES); ?>">
<input name="query" type="text" value="some_data">
<input name="send" type="submit" value="send">
</form>

Categories