I want to submit a form that have many checkboxes with different values, using firebug I am sure that the checkboxes values are send to the server, but when I print_r the values of the post variable, only the last value is printed and igoring the other values.
<input type="checkbox" name="fruits" value="apple" />
<input type="checkbox" name="fruits" value="orange" />
<input type="checkbox" name="fruits" value="banana" />
and in the server when I print the request parameters
$formData = $this->getRequest()->getPost();
print_r($formData);
Only the last option is printed even I choose all of them and firebug shows them all!
I solve it by making [] in the name of the checkboxex
<input type="checkbox" name="fruits[]" value="apple" />
<input type="checkbox" name="fruits[]" value="orange" />
<input type="checkbox" name="fruits[]" value="banana" />
This is ZF way of doing the same
$fruits = new Zend_Form_Element_MultiCheckbox('fruits', array(
'multiOptions' => array(
'apple' => 'Label for apple',
'orange' => 'I am good orange',
'banana' => 'I am banana'
);
));
echo $fruits;
Related
i generate radio buttons using Yii
the code looks like
<span class="gender">
<?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'male')); ?>
</span>
<span class="gender">
<?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'female')); ?>
</span>
It's generate next HTML code
<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">
<input value="male" class="male" name="web\models\register[gender]"id="web\models\register_gender" type="radio"></span>
<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">
<input value="female" class="female" name="web\models\register[gender]" id="web\models\register_gender" type="radio">
</span>
when i get POST from this form, if i checked female it returns female, but if i cheked male it returns 0. I think it is because of identical inputs ids. But how can i avoid it?
You can configure activeRadioButton to not output the "guard input" with value 0. Do this by passing the parameter 'uncheckValue' => null as part of your options array:
echo CHtml::activeRadioButton($model,'gender',
array('value' => 'male', 'uncheckValue' => null));
echo CHtml::activeRadioButton($model,'gender',
array('value' => 'female', 'uncheckValue' => null));
That said, using activeRadioButtonList is the best choice here. You can configure its template parameter to specify your custom HTML:
$genders = array('male' => 'male', 'female' => 'female');
$options = array(
'template' => '<span class="gender">{input</span>',
'uncheckValue' => null,
);
echo CHtml::activeRadioButtonList($model, 'genger', $genders, $options);
I have a problem with label decorator. When I generate a form I have something like this:
<label id="user_email-label"><label for="user_email" class="required">e-mail</label>
</label>
<input type="text" name="user_email" id="user_email" value="" class="span4">
but I need :
<label for="user_email" class="required">e-mail</label>
<input type="text" name="user_email" id="user_email" value="" class="span4">
My code:
$oText = new Zend_Form_Element_Text('user_email');
$oText->clearDecorators();
$oText->setLabel($oTranslate->translate('e-mail'));
$oText->setAttrib('class','span4');
$oText->setRequired(true);
$oText->addValidator('NotEmpty', true);
$oText->addValidator('StringLength', true, array('max' => 200));
$oText->addValidator('EmailAddress', true);
$oText->addDecorator('ViewHelper');
$oText->addDecorator('Errors');
$oText->addDecorator('Label',array('tag'=>'label', 'placement' => 'prepend'));
$oText->setFilters(array('StringToLower','StringTrim'));
$this->addElement($oText);
And my second question is how can I put a checkbox inside label? When I am doing something like this:
$oCheckbox = new Zend_Form_Element_Checkbox('remember_me');
$oCheckbox->setLabel($oTranslate->translate('remember me'));
$oCheckbox->setChecked(true);
$oCheckbox->addDecorator('HtmlTag',array('tag'=>'label','class'=>'checkbox muted'));
$oCheckbox->removeDecorator('DtDdWrapper');
$oCheckbox->addDecorator('Label',array('tag'=>'label'));
$this->addElement($oCheckbox);
I have:
<label class="checkbox muted">
<input type="hidden" name="remember_me" value="0"><input type="checkbox" name="remember_me" id="remember_me" value="1" checked="checked"></label>
<label id="remember_me-label"><label for="remember_me" class="optional">remember me</label></label>
but I need:
<label class="checkbox muted">
<input type="hidden" name="remember_me" value="0"><input type="checkbox" name="remember_me" id="remember_me" value="1" checked="checked">
remember me</label>
Any ideas?
Regards
The part where you build your element, the 'tag' option
$oText->addDecorator('Label',array('tag'=>'label', 'placement' => 'prepend'));`
is not needed. Change it to:
$oText->addDecorator('Label',array('placement' => 'prepend'));
As for the second question, I think you would have to write your custom element to achieve that. Alternatively, you can use the view script to render each part of the element as you like.
The answer to your second query is to use the IMPLICIT_APPEND placement on the label decorator.
$oCheckbox->addDecorator('Label',array('placement' => 'IMPLICIT_APPEND'));
trying to create a form that has two submit buttons in cakephp.
task_1 submit button when clicked creates this in the address bar '/fields/add' and sends the information added to the database.
task_2 submit button when clicked creates this in the address bar '/fields/add/add' and sends the entered information to the database`.
task_1 is meant to allow users to keep creating new fields, where task 2 is meant to take them to a different page, at the moment we just chose a page at random.
here is the code for the add function in controller
function add(){
$this->set('title_for_layout', 'Please Enter Your Invoice Headings');
$this->set('stylesheet_used', 'style');
$this->set('image_used', 'eBOXLogo.jpg');
$this->Session->setFlash("Please create your required fields.");
if($this->request->is('post')){
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->params['form']['task_1'] == "task_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
if($this->params['form']['task_2'] == "task_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Invoices','action' => 'add'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
here is the code for the add view
<form enctype="multipart/form-data" method="post" action="add/">
Name: <input type ="text" name="name" /> <br />
Description: <input type ="text" name="description" /> <br />
Account ID: <input type ="number" name="accounts_id" /> <br />
<br />
<input type="submit" name="task_1" value="Continue adding fields" />
<input type="submit" name="task_2" value="Finish adding fields" />
</form>
echo $this->Form->create('Field');
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('account_id');//this would be the conventional fk fieldname
echo $this->Form->button('Continue adding fields', array('name' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'type_2'));
echo $this->Form->end();
I have this group of radio buttons in which each of individual button has of its own position set through style attribute. I would like to how can I archive the same by using drupal form api. I found how to style as whole but, not as individual control within group. Here's how my html code look like -
<input type="radio" name="base_location" checked="checked" value="0" style="margin-left:70px;float:left;"/><span style="float:left;">District</span>
<input type="radio" name="base_location" value="1" style="margin-left:50px;float:left;"/><span style="float:left;">MRT</span>
<input type="radio" name="base_location" value="2" style="margin-left:60px;float:left;"/><span style="float:left;">Address</span>
And this is the drupal code I'm stuck at -
$form['base_location'] = array(
'#type' => 'radios',
'#title' => t('base location'),
'#default_value' => variable_get('search_type', 0),
'#options' => array(
'0'=>t('District'),
'1'=>t('MRT'),
'2'=>t('Address')),
'#description' => t('base location'),
I'm aware of #type=>radio existence. However, I do not know how to group all my radio buttons together in this regard. If I use same array key for all of them, they would collide each other. If I don't, they aren't seen as part of the same group. I thank you in advance.
If you're using Drupal 6.x Form API and #type=>radios (http://api.drupal.org/api/function/theme_radios/6) each radio element will have its unique id which you can use to apply proper CSS.
The example you provided
$form['base_location'] = array(
'#type' => 'radios',
'#title' => t('base location'),
'#default_value' => variable_get('search_type', 0),
'#options' => array(
'0'=>t('District'),
'1'=>t('MRT'),
'2'=>t('Address')),
'#description' => t('base location'),
);
should output markup like so:
<div id="base-location-0-wrapper" class="form-item">
<label for="base-location-0" class="option"><input type="radio" class="form-radio" value="0" name="base_location" id="base-location-0"> District</label>
</div>
<div id="base-location-1-wrapper" class="form-item">
<label for="base-location-1" class="option"><input type="radio" class="form-radio" value="1" name="base_location" id="base-location-1"> MRT</label>
</div>
<div id="base-location-2-wrapper" class="form-item">
<label for="base-location-2" class="option"><input type="radio" class="form-radio" value="2" name="base_location" id="base-location-2"> Address</label>
</div>
Apply the following CSS and you should be set.
#base-location-0-wrapper,
#base-location-1-wrapper,
#base-location-2-wrapper {
display:inline;
margin-left:50px;
}
The answer is to use CSS with the html that you already have. It looks like you just want to change the radio buttons' display to 'inline' with 10px margins, which you can do. (And if you did break the radios up into separate elements within a fieldset, they'd no longer interact with each other.)
I'm totally confused about how decorators work. This is the html structure that I'm trying to achieve:
<form id="" action="" method="post">
<fieldset><legend>Contact form</legend>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" size="30" />
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" id="email" size="30" />
</p>
<p>
<label for="web">Website</label>
<input type="text" name="web" id="web" size="30" />
</p>
<p>
<label for="message">Message</label>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
</p>
<p class="submit"><button type="submit">Send</button></p>
</fieldset>
</form>
How do I get rid of the definition list format and use a paragraph tag based layout instead?
Update: Is there a way for me to apply this style to all the forms I have created? instead of having to apply the decorators to each and every form?
If you want to change the elements of your form you have to reset the Decorators of your Form and it's elements.
Example of enclosing a field in a p-tag
class Default_Form_Contact extends Zend_Form
{
public function init()
{
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name:')
->setDecorators(
array(
array('ViewHelper', array('helper' => 'formText')),
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'p', 'id' => 'name-element')),
array('Label', array('class' => 'label')),
)
);
$this->addElement($name);
}
}
Which decorators you really need you have to consider yourself. For the form decorators you can do in the init()
$this->setDecorators(array(some decorators));
After a while, I just gave up and used just the ViewRenderer decorator. Here is a good post explaining how to do that (http://weierophinney.net/matthew/archives/215-Rendering-Zend_Form-decorators-individually.html). The rest of that series is good as well if you really want to know how to use decorators.
With the ViewRenderer decorator, you're basically rendering your form against a template (not unlike MVC itself). This way gives you the ultimate control over everything, but of course what you gain in flexibility, you lose in RAD, and you make up in complexity.