How to disbale select box in zend framework - zend-framework

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

Related

Zend Framework 2 Radio Button null value default

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.

how to add decoratives in select box in zend framework Form

I am new to zend I want to add decoratives,size in my select box
example like this
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:103px'),
'multiOptions' => $list,
'decorators'=>Array(
'ViewHelper','Errors'
),
this is my code
$companyName = new Zend_Form_Element_Select('companyName');
$this->addElement($companyName);
Your question isn't very clear, but you can supply an array of options as the second parameter to the form element:
$companyName = new Zend_Form_Element_Select('companyName', array(
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement($companyName);
This tutorial will help you
http://www.codexperience.co.za/post/creating-good-looking-zend-forms
Its all about styling your zend forms using the traditional way whilst retaining the awesomeness that Zend_Form comes with

How can I render label to the right of the element?

I am adding a consent checkbox to an existing form. I am not able to render the label to the right of the checkbox. What am I doing wrong?
Please note that the check box has created using $this->addElement( because the rest of the form was created this way.
I thank you in advance.
$this->addElement('checkbox', 'isUserConsent', array(
'options' => array(array('placement' => 'APPEND')),
'label' => 'Plz activate',
'validators' => array(
array('InArray', false, array(
'hay' => array(1),
'messages' => 'Please check the consent box'),
)
),
'decorators' => array('ViewHelper','Errors','Label'),
));
The default is to prepend the label, but you can change this by modifying the decorator's 'placement' option:
$this->getElement('isUserConsent')->getDecorator('label')->setOption('placement', 'append');
Edit: I never use this syntax for decorators but it should be something like this:
$this->addElement('checkbox', 'isUserConsent', array(
'options' => array(array('placement' => 'APPEND')),
'label' => 'Plz activate',
'validators' => array(
array('InArray', false, array(
'hay' => array(1),
'messages' => 'Please check the consent box'),
)
),
'decorators' => array(
'ViewHelper',
'Errors',
'Label' => array(
'placement' => 'append'
)
),
));
This is the code for rendering lable of a form element.
$this->form->name->renderLabel() ;

How to validate a checkbox in ZF2

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],
],
],
],
])

zend helper Db_NoRecordExists for multiple table check

I want to check mobile number uniqueness in my two table ..I have added this code but It checking only second one...Is it any other way to validated this in form..
$this->addElement('text', 'mobilenumber', array(`enter code here`
'filters' => array('StringTrim'),
'validators' => array`enter code here`(
array('Db_NoRecordExists', true, array('table' => 'beroe_user', 'field' => 'mobilenumber', 'messages' => array(
'recordFound' => 'mobilenumber already exists'
))),
array('Db_NoRecordExists', true, array('table' => 'beroe_user', 'field' => 'mobilenumber', 'messages' => array(
'recordFound' => 'admin already exists'
))),
),
// 'required' => true,
'label' => 'Phone ',
'maxlength' => '15'
));
I think this is because when we add same validator to an element multiple times the former will be overridden. Check class Zend_Form_Element, addValidator() line 1153
You can create a custom validators as you need. It would be the best thing to do.