I am using Zend Framework 2, Doctrine Module and SQLServer to build a number of products.
I have a question in relation to Zend\Form\Radio.
I have the following defined in a form:
// boolean $disabled_access
$this->add(array(
'name' => 'disabled_access',
'type' => 'radio',
'options' => array(
'label' => 'Disabled Access',
'value_options' => array('1'=>"Yes", '0' => 'No'),
'allow_empty' => true,
'nullable' => true,
),
'attributes' => array('value' => null),
));
It is bound to a $building entity.
If the value of 'disabled_access' is set to true in the DB, the radio button renders correctly. Similarly if it's is set to false.
However, if the column has a NULL value, the radio button defaults to 'No'. How do I set it up to show all three potential results?
This should be enough -
$this->add(array(
'name' => 'disabled_access',
'type' => 'radio',
'options' => array(
'label' => 'Disabled Access',
'value_options' => array('1'=>'Yes', '0' => 'No'),
),
)
);
If value in database is -
1 -> 'Yes' radio button will be selected,
0 -> 'No' radio button will be selected,
NULL -> none of them will be selected.
Its working for me.
I hope it helps.
Related
I have created a form on a Symfony CRM using the buildForm() function in a form type file. This form includes a choice drop down consisting of simple "yes" and "no" options which map to 1 and 0 respectively. I need to be able to have "no" as the default as my client more often will select this option over the "yes". After reading the documentation here I figured that the preferred_choices option would suit my needs.
Here is my entry in the buildForm() :
$builder->add('non_rider', ChoiceType::class,
array(
'label' => 'Is Non-Rider',
'required' => true,
'placeholder' => false,
'choices' => array(
'Yes' => 1,
'No' => 0
),
'preferred_choices' => array(0,1),
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control required'
)
));
However, this brings out the order as "Yes" then "No" with "Yes" as the default selected option. I was wondering if it reads 0 as null, which means it doesn't register? Is there any way to make "No" the auto-selected option on form load?
You can use the "data" option as mentioned here symfony.com/doc/current/reference/forms/types/choice.html, and show in action here http://stackoverflow.com/a/35772605/2476843
$builder->add('non_rider', ChoiceType::class,
array(
'label' => 'Is Non-Rider',
'required' => true,
'placeholder' => false,
'choices' => array(
'Yes' => 1,
'No' => 0
),
'data' => 0,
'preferred_choices' => array(0,1),
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control required'
)
));
Good evening,
I have the following option in Redux Framework (version 3.1.9):
array(
'id' => 'social-networks',
'type' => 'sortable',
'mode' => 'checkbox',
'title' => __('Order Social Icons', 'options'),
'subtitle' => __('Order and activate/deactivate social icons', 'options'),
'options' => array(
'fb' => 'Facebook',
'tw' => 'Twitter',
'lin' => 'LinkedIn',
)
),
And the output (if all three options are checked) is:
[fb] => 1
[tw] => 1
[lin] => 1
After this option I want to display a text field for every checked social network in this list and I have the example text input:
array(
'id' => 'facebook-url',
'type' => 'text',
'required' => array('social-networks["fb"]','equals','1'),
'title' => __('Facebook URL', 'options'),
'subtitle' => __('Enter your Facebook URL.', 'options'),
),
Of course the "required" argument doesn't work.
Is there a way to use this argument by using an option from a field and not only the ID.
Thanks
According to http://docs.reduxframework.com/redux-framework/arguments/required/, your conditional option should look like this:
array(
'id' => 'facebook-url',
'type' => 'text',
'required' => array('social-networks','equals','fb'),
'title' => __('Facebook URL', 'options'),
'subtitle' => __('Enter your Facebook URL.', 'options'),
),
Next option:
array(
'id' => 'twitter-url',
'type' => 'text',
'required' => array('social-networks','equals','tw'),
'title' => __('Twitter URL', 'options'),
'subtitle' => __('Enter your twitter URL.', 'options'),
),
and so on.
Hi I am new in zend framework. I want to disable my select box when my status value(from database) is 0. Basically I don't know the syntax Please help me.
This is my select box code:
$sub= new Zend_Form_Element_Select('subject',array(
'id' => 'sub',
'required' => true,
'filters' => array('StringTrim'),
'multiOptions' => array(
'0' => '---Select---',
),
'style' => array('width:103px'),
'value' => $sub_val,
'decorators'=>Array(
'ViewHelper','Errors'
),
));
$subject_values->addMultiOption('subject1','test');
$this->addElement($subject_values)
$sub->setAttrib('disabled','disabled'); will disable the select box subject
Here is my code:
class File_Form_AddFile extends Custom_Form {
public function init() {
$translate = Zend_Registry::get('translate');
$this->setTranslator($translate);
$this->setName("addfile");
$this->setMethod('post');
$this->addElement('text', 'title', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 50)),
),
'required' => true,
'label' => __('Title') . ':',
));
$this->addElement('radio', 'type', array(
'label'=>__('Applicant Type'),
'multiOptions' => array(
'office' => 'Office',
'community' => 'Community',
'person' => 'Person',
),
'required' => true,
'separator' => '',
'value' => 'office'
));
**// I want this section to show only after 'community' is clicked at above input field.**
$this->addElement('radio', 'community_is_registered', array(
'label'=>__('Registered Community?'),
'multiOptions' => array(
1 => 'Yes',
0 => 'No',
),
'separator' => '',
'value' =>'0'
));
$this->addElement('text', 'name', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 100)),
),
'required' => true,
'label' => __('Applicant Name') . ':',
));
$this->addElement('submit', 'save', array(
'required' => false,
'ignore' => true,
'label' => __('Save'),
));
$this->save->removeDecorator('label');
}
}
This is a form to add some information of a file. Here i want to show the section of "Registered Community?" only after the button "Community" is clicked at "Applicant Type". Seeing for help !!
Since you want certain behavior to occur on the client-side based upon certain actions taking place on the client-side, it seems to be fundamentally a client-side question.
Perhaps the easiest thing is to:
Add a certain CSS class to the Registered Community element (or display group, if it's a collection of elements) during server-side form creation.
Use front-end CSS to hide all form elements of that class.
Add a client-side on-click handler to the "Applicant Type" element that removes/changes the class or shows/hides the "Registered Community" section when the "Applicant Type" has the desired value.
I've read numerous workarounds for Zend Framework's lack of default checkbox validation.
I have recently started using ZF2 and the documentation is a bit lacking out there.
Can someone please demonstrate how I can validate a checkbox to ensure it was ticked, using the Zend Form and Validation mechanism? I'm using the array configuration for my Forms (using the default set-up found in the example app on the ZF website).
Try this
Form element :
$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'
),
));
In filters, add digit validation
use Zend\Validator\Digits; // at top
$inputFilter->add($factory->createInput(array(
'name' => 'agreeterms',
'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.',
),
),
),
),
)));
You could also just drop the hidden form field (which I find a bit weird from a purist HTML point of view) from the options instead of setting its value to 'no' like this:
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to all terms and conditions',
'use_hidden_element' => false
),
));
I had the same problem and did something similar to Optimus Crew's suggestion but used the Identical Validator.
If you don't set the checked_value option of the checkbox and leave it as the default it should pass in a '1' when the data is POSTed. You can set it if you require, but make sure you're checking for the same value in the token option of the validator.
$this->filter->add(array(
'name' => 'agreeterms',
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => '1',
'messages' => array(
Identical::NOT_SAME => 'You must agree to the terms of use.',
),
),
),
),
));
This won't work if you use the option 'use_hidden_element' => false for the checkbox for the form. If you do this, you'll end up displaying the default NotEmpty message Value is required and can't be empty
This isn't directly related to the question, but here's some zf2 checkbox tips if you're looking to store a user's response in the database...
DO use '1' and '0' strings, don't bother trying to get anything else to work. Plus, you can use those values directly as SQL values for a bit/boolean column.
DO use hidden elements. If you don't, no value will get posted with the form and no one wants that.
DO NOT try to filter the value to a boolean. For some reason, when the boolean value comes out to be false, the form doesn't validate despite having 'required' => false;
Example element creation in form:
$this->add([
'name' => 'cellPhoneHasWhatsApp',
'type' => 'Checkbox',
'options' => [
'label' => 'Cell phone has WhatsApp?',
'checked_value' => '1',
'unchecked_value' => '0',
'use_hidden_element' => true,
],
]);
Example input filter spec:
[
'cellPhoneHasWhatsApp' => [
'required' => false,
],
]
And here's an example if you want to hide some other form fields using bootstrap:
$this->add([
'name' => 'automaticTitle',
'type' => 'Checkbox',
'options' => [
'label' => 'Automatically generate title',
'checked_value' => '1',
'unchecked_value' => '0',
'use_hidden_element' => true,
],
'attributes' => [
'data-toggle' => 'collapse',
'data-target' => '#titleGroup',
'aria-expanded' => 'false',
'aria-controls' => 'titleGroup'
],
]);
I'm a ZF2 fan, but at the end of the day, you just have to find out what works with it and what doesn't (especially with Forms). Hope this helps somebody!
Very old question, but figured it might still be used/referenced by people, like me, still using Zend Framework 2. (Using ZF 2.5.3 in my case)
Jeff's answer above helped me out getting the right config here for what I'm using. In my use case I require the Checkbox, though leaving it empty will count as a 'false' value, which is allowed. His answer helped me allow the false values, especially his:
DO NOT try to filter the value to a boolean. For some reason, when the boolean value comes out to be false
The use case is to enable/disable certain entities, such as Countries or Languages so they won't show up in a getEnabled[...]() Repository function.
Form element
$this->add([
'name' => 'enabled',
'required' => true,
'type' => Checkbox::class,
'options' => [
'label' => _('Enabled'),
'label_attributes' => [
'class' => '',
],
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 0,
],
'attributes' => [
'id' => '',
'class' => '',
],
]);
Input filter
$this->add([
'name' => 'enabled',
'required' => true,
'validators' => [
[
'name' => InArray::class,
'options' => [
'haystack' => [true, false],
],
],
],
])