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'
),
));
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 am setting up a test site with symfony and I built a datagrid with pagination that works correctly.
One standard feature I would like to add to pagination is the number of displayed items to be set for a dropdown menu:
I have a choice field (for the moment in a controller, but later I will put it in a form class.)
Form with the choice field in built using this code:
$form = $this->createFormBuilder()
// ->setAction($this->generateUrl('userlist2'))
//->setMethod('GET')
->add('numRecords', 'choice', array(
'choices' => array('5' => '5', '10' => '10', '20' => '20'),
'required' => false,
'empty_value' => false,
'attr' => array(
'class' => 'form-control',
'style' => 'width:80px',
)
)
)
->getForm();
I have googled to check if symfony has a way to get changes in choice field but could not find it.
What I need is to intercept the change event of the choice field and get its value, so I can put the item number value in a variable and use it for pagination.
Any help appreciated
Thanks
Paolo
I've got a (hopefuly) simple task, and my google fu has failed me. Basically, I've got a form with a select which contains an empty value and then number of ids given content can belong to. What I want to do is - validate if the given ids exist, but only if a value is set. This:
$field = new Zend_Form_Element_Select('field');
$field->addValidator(
new Zend_Validate_Db_RecordExists(
array(
'table' => 'categories',
'field' => 'id'
)
));
takes care of the checking if the given id exists, but I'm not able to find any way to omit this if value is empty. One way to do this would be to move this logic to isValid method, but I'm hoping there's nicer way to accomplish this task.
Try to set this form element as not required:
$field->setRequired(false);
When element is not required and is not filled, validators queue won't be run.
Quick example which works for me:
// Zend_Form form body
$this->addElement('select', 'category', array(
'label' => 'Choose category',
'required' => false,
'multiOptions' => array(
null => 'No category selected',
'1' => 'One',
'2' => 'Two',
),
'validators' => array(
array('Db_NoRecordExists', false, array(
'schema' => 'public',
'table' => 'category',
'field' => 'id',
)),
),
));
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.
I am trying to have a form with float number validation.
when validation works it won't let me click the submit button and will show the proper error message.
I am using zend framework 2 and in my Form I want to retrieve alcohol volume.
I'm trying to use the following code:
$this->add($factory->createElement(array(
'name' => 'alcohol_vol',
'attributes' => array(
'label' => 'alcohol vol%:',
'filters' => array('Float'),
'type' => 'text',
'required' => true,
),
)));
this doesn't do anything actually. it will pass validation if i enter regular text.
I also tried changing the type to 'Number' from 'text' but then it won't allow me to use floating number. it will allow only none-float numbers :)
There is no "Float" filter in ZF2, I guess may you want is "Float" Validator, Float Validator could be add into ZF2 form like this:
$this->add($factory->createElement(array(
'name' => 'alcohol_vol',
'attributes' => array(
'label' => 'alcohol vol%:',
'type' => 'text',
),
)));
$factory = new Zend\InputFilter\Factory();
$this->setInputFilter($factory->createInputFilter(array(
'alcohol_vol' => array(
'name' => 'alcohol_vol',
'required' => true,
'validators' => array(
array(
'name' => 'Float',
),
),
),
)));
Then you should validate form in controller, above validators should still set into form. If input not float, the input element will have invalidate messages:
$form->setData($userInputData);
if (!$form->isValid()) {
$inputFilter = $form->getInputFilter();
$invalids = $inputFilter->getInvalidInput();
var_dump($invalids);
// output: 'abc' does not appear to be a float
}
I think you can use this filter
new Zend\I18n\Filter\NumberFormat("en_US", NumberFormatter::TYPE_DOUBLE);
I recommend the Zend\I18n\Validator\Float class. Example usage:
$floatInput = new Input('myFloatField');
$floatInput->getValidatorChain()
->attach(new \Zend\I18n\Validator\Float());
See:
http://framework.zend.com/manual/2.3/en/modules/zend.input-filter.intro.html
http://framework.zend.com/manual/2.0/en/modules/zend.validator.set.html#validating-digits