Zend Framework 2 Checkbox setValue for unchecked not work - php

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.

Related

CakePHP 3.x Change Label For Radio Buttons

I am attempting to create several radio buttons with a specific label...
I am using the following code, however, there is no label
echo $this->Form->radio('qualify', [['value' => 1, 'text' => 'Yes'], ['value' => 0, 'text' => 'No']], ['label' => 'Qualify Only']);
Any ideas on how I can get the label to show up for these radio buttons?
<?php
$qualify = [['value'=>'0','text'=>'No'],['value'=>'1','text'=>'Yes']];
echo $this->Form->input('qualify', array(
'options' => $qualify,
'type' => 'radio',
'placeholder' => 'Qualify Only'
));
?>
This code will be able to pick any existing value in the edit view.
You can still use the $this->Form->input and put a type whether its a select or radio
<?php
$qualify = ['0' => 'No', '1' => 'Yes'];
echo $this->Form->input('qualify', array(
'options' => $qualify,
'type' => 'radio',
'placeholder' => 'Qualify Only'
));
?>

how to make hyperlink to label in zf2

I want to make a hyperlink of label in zend form. I agree to the Terms and Privacy policy make terms and condition to be link.
My code is:-
$this->add(array(
'type' => 'Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to the Terms and Privacy policy',
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 'no',
'id'=>'term_and_condition',
),
));
You can split label and input field in the view:
$this->formLabel($this->form->get('agreeterms'));
$this->formText($this->form->get('agreeterms'));
Then you can wrap the ... around it.

Before submit at least one checkbox is required

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

Zendframework 2 Checkox

im pretty new to ZF2 and i have a pretty strange problem. I´ve created a simple checkbox using arrays,
and set checked value to good and unchecked value to bad. But when i submit my form, the URL shows that when the checkbox is checked, it sends .....checkbox=bad&checkbox=good... I don´t know why.
class SearchForm extends Form {
public function __construct($name = null){
parent:: __construct('Search');
$this->setAttribute('method', 'get');
$this->setAttribute ( 'enctype', 'multipart/formdata' );
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
'options' => array(
'label' => 'A checkbox',
'checked_value' => 'good',
'unchecked_value' => 'bad',
),
));
Because by default Zend\Form\Element\Checkbox has use_hidden_element is true.
If set to true (which is default), the view helper will generate a
hidden element that contains the unchecked value. Therefore, when
using custom unchecked value, this option have to be set to true.
You use GET method. Of couse, you see two values in query string: for checkbox and for hidden elements.
See more carefully ZF2#Checkbox.
Not 100% sure this is the answer to the issue and not in a position to test right now but it is likely to do with the hidden element that the checkbox uses try:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
'options' => array(
'label' => 'A checkbox',
'use_hidden_element' => false,
'checked_value' => 'good',
'unchecked_value' => 'bad'
),
));

How to reload a form from a drop down

I would like to reload the form, or preferably just the values, by using 'onChange'. There are several elements in the form that needs to be updated when changing the project id.
$this->addElement('select', 'project_id', array(
'label' => t('Project'),
'multiOptions' => $data,
'filters' => array('Int'),
'value' => 0,
'required' => true,
'disableTranslator' => true,
'validators' => array(),
'onChange' => 'Javascript:window.location.reload(false)'
));
You can change your onChange to call a custom javascript function instead - which will change any fields you need to change, and then submmit the form.

Categories