I have shown multiple checkboxes. When I submit I need at least one checkbox is required.
I tried this:
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => Mage::helper('listings')->__('Select Categories'),
'name' => 'cat_ids[]',
'values' => $categorieslist,
'onclick' => "",
'onchange' => "",
'value' => $cat_ids,
'disabled' => true,
'class' => 'category_match_to_listing',
'required' => true,
));
even class name also not displayed in html
<input type="checkbox" value="14" name="cat_ids[]" id="checkboxes_id">
If I use one value 'values' => 'single', required field working fine, then the html is
<input type="checkbox" value="0" class="category_match_to_listing required-entry" name="cat_ids[]" id="checkboxes_id">
When I get dynamic it doesn't work. Please suggest any idea.
Use validate-one-required instead of required entry
Related
I tried set checkbox value in false:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'test_checkbox',
'options' => array(
'label' => 'Test checkbox',
'use_hidden_element' => false,
'checked_value' => 1,
'unchecked_value' => 0,
),
'attributes' => array(
'value' => 0,
),
));
But as a result of this page contains:
<input type="checkbox" name="test_checkbox" value="1">
The value does not change and I can not understand why.
Other PHP and JS script not change this value.
Maybe I misunderstood how "checked_value", "unchecked_value" and "value" works?
The code you have is almost correct. It should be:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'test_checkbox',
'options' => array(
'label' => 'Test checkbox',
'use_hidden_element' => false,
'checked_value' => 1,
'unchecked_value' => 0,
)
));
But I think you misunderstand how HTML checkboxes work. The value attribute should always contain only the checked value. Browsers only submit this value if the checkbox is ticked. So when the page loads, the checkbox will correctly appear in the source as:
<input type="checkbox" name="test_checkbox" value="1">
To achieve the unchecked value, ZF (and all other frameworks I know of) add it to a hidden form field above the checkbox. If the checkbox is not ticked, the browser will submit the hidden form field instead.
I want to make a chekbox for terms of use. I've found an interesting solution, given by a webcoder How to validate a checkbox in ZF2
The case is that I have a difficult HTML structure, that's why I can't use neither formRow, nor formCollection to render. I'm trying to use the following approach instead:
$agreement = $form->get('agreement');
echo $this->formInput($agreement);
echo $this->formElementErrors($agreement);
And I receive the empty value:
<input type="checkbox" name="agreement" class="checkbox" value="">
I also tried to add hidden field, but my attempt also failed:
$agreement = $form->get('agreement');
echo $this->formHidden($agreement);
echo $this->formInput($agreement);
echo $this->formElementErrors($agreement);
The html is:
<input type="hidden" name="agreement" class="checkbox" value="">
<input type="checkbox" name="agreement" class="checkbox" value="">
As a result I receive errors about "Value is required and can't be empty"
May be someone can give me a tip how to properly render checkbox in my case?
code of my inputFilter:
<?php
namespace Auth\Form;
use Zend\InputFilter\InputFilter;
use Zend\Validator\Digits;
class UserFormFilter extends InputFilter {
public function __construct() {
$this->add(array(
'name' => 'agreement',
'validators' => array(
array(
'name' => 'Digits',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array (
Digits::NOT_DIGITS => 'You must agree to the terms of use.',
)
),
),
),
));
}
}
?>
Check this out please.
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to all terms and conditions',
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 'no'
),
'attributes' => array(//the difference is here
'value' => 'yes'
)
));
if you add the attributes array it should work.
I need to generate Radio button with cake php format like this
<div class="radioset gender">
<div>
<div>Male</div>
<label>
<input type="radio" name="sex" value="1"/>
</label>
</div>
<div>
<div>Female</div>
<label>
<input type="radio" name="sex" value="0"/>
</label>
</div>
I can not use
e($form->radio("User.sex",array(0=>"Male",1=>"Female"),array("legend"=>false)));
Because it can not generate HTML format that i want.
I use multi input like that
e($form->input( 'User.sex', array('type'=>'radio', 'options' => array(0=>""),'div'=>false, "error"=>false, 'label' => false ) ));
e($form->input( 'User.sex', array('type'=>'radio', 'options' => array(1=>""),'div'=>false, "error"=>false, 'label' => false ) ));
But when i submit form. Server can not get value.
Please help me how to do it.
Thanks.
Use this format:
echo $form->input('field', array(
'type' => 'radio',
'legend'=>'Group of Radio',
// 'after' => '--after--',
// 'between' => '--between---',
'separator' => '--separator--',
'default' => '--which is by default selected--',
'options' => array('Button One', 'Button Two')
));
you can do this with Separator. I.e
echo $this->Form->input('name', array( 'type' => 'radio',
'before' => '<div>',
'separator'=> '</div><div>',
'after' => '</div>',
'options' => $option,
'label' => true,
"legend" => false
)
);
I am using the last version of cakephp, with the follow code I have to create a list of checkboxes.
echo $this->Form->input('regions', array(
'type' => 'select',
'hiddenField' => false,
'options' => $regions,
'multiple' => 'checkbox',
'div' => false
));
the code works 90%, I mean... the list has been created BUT I still see <div>
This is the result:
<div class="checkbox"><input type="checkbox" name="data[regions][]" value="1" id="Regions1" /><label for="Regions1">AAAA</label></div>
<div class="checkbox"><input type="checkbox" name="data[regions][]" value="2" id="Regions2" /><label for="Regions2">BBBB</label></div>
<div class="checkbox"><input type="checkbox" name="data[regions][]" value="3" id="Regions3" /><label for="Regions3">CCCC</label></div>
The result I need is:
<li>
<input type="checkbox" name="data[regions][]" value="1" id="Regions1" /><label for="Regions1">AAAA</label>
</li>
...
How can I do it ?
By setting 'div' => false, your prevents creating a <div> around the whole input section (i.e. the set of checkboxes). But obviously you want to disable the div's around the option. Unfortunately, I have not been able to find a way to disable this with Cake.
However, you can simulate the <li> items with some CSS trickery. Encapsulate the inputs in a div with a special class (the opposite of what you are doing now), then use CSS to force using <li> styling:
In your CSS:
.box2li div
{
display: list-item;
}
In your Cake view:
echo $this->Form->input('regions', array(
'type' => 'select',
'hiddenField' => false,
'options' => $regions,
'multiple' => 'checkbox',
'div' => array ('class' => 'box2li')
));
Each checkbox is now preceded by a... ...bullet
There should be a more simple way but you can always do it in the traditional way :)
while (list($key, $value) = each($regions)){
echo '<li>'.
$this->Form->input($value,
array(
'type' => 'checkbox',
'name' => 'data[regions][]',
'div' => false,
'value' => $key,
'label' => false,
'after' => $this->Form->label($value, $value)
))
.'</li>';
}
Not so beautiful, but works :)
Use Form->Checkbox instead of Form->input
$this->Form->checkbox('', array(
'label' => false,
'div' => false,
'class' => ''
));
$this->Form->checkbox('', array(
'label' => false,
'div' => false,
'class' => ''
));
$this->Form->checkbox('', array(
'label' => false,
'div' => false,
'class' => ''
));
I'm adding forms to my page using Zend/Form.
I'm adding elements by defining them as follows:
$this->add(array(
'name' => 'value',
'attributes' => array(
'type' => 'text',
'id' => 'value',
'autocomplete' => 'off',
'placeholder' => 'Cost',
),
'options' => array(
'label' => 'Cost',
),
));
As you can see there is a 'label' => 'cost' node, this generated a label to go with the input element.
How do I add classes, attributes to this label ?
Please try this, i haven't tested or used this, but going by the source it should function properly:
$this->add(array(
'name' => 'value',
'attributes' => array(),
'options' => array(
'label_attributes' => array(
'class' => 'mycss classes'
),
// more options
),
));
If this does not function, please leave me a comment. If it won't function, it is not possible using this approach, since the FormLabel restricts the validAttributes quite a bit:
protected $validTagAttributes = array(
'for' => true,
'form' => true,
);
This works well in Zend Framework 2.3 :
$this->add(array(
'name' => 'userName',
'attributes' => array(
'type' => 'text',
'class' => 'form-control',
'placeholder' =>'Username',
),
'options' => array(
'label' => 'Username',
'label_attributes' => array('class' => 'control-label')
),
));
$element->setOptions(array('label_class' => array('class' => 'control-label')));
Produces code like this:
<label class="control-label">
<input type="radio" name="option1" id="option1" value="1">
Option 1
</label>
<label class="control-label">
<input type="radio" name="option2" id="option2" value="2">
Option 2
</label>
I have tried this. It works in Zend Framework One.
Note if you use
$element->setOptions(array('label_attributes' => array('class' =>
'control-label')));
you get the undesirable effect for some reason of
<label attributes="control-label">
<input type="radio" name="option1" id="option1" value="1">
Option 1
</label>
For programmatic approach on ZF2+ try this:
$element->setOptions(array(
'label_attributes' => array(
'style' => 'color:gray;'
)
));
Inspired by Damon's answer.