how to set up a zend multiCheckbox form field with the checkboxes checked? - zend-framework

i have this form:
$this->addElement (
'multiCheckbox', 'servers2',
array (
'checkedValue' => '0',
'multiOptions' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
));
the problem is that the checkedValue doesn't work for this setup, it does for a simple checkbox. I've also tried 'checkedValues' => array('1','0'), singular or plural,
but no end in sight.
any ideas?
THanks

To mark certain checkboxes as checked, try this:
$multiCheckElement->setValue(array('11.com', '3.com'));
// or
$this->addElement (
'multiCheckbox', 'servers2',
array (
'value' => array('11.com', '3.com'), // select these 2 values
'multiOptions' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
)
);
See also Zend_Form_Element_MultiCheckbox

ZF2 will require you to use value_options;
$form->add(
array(
'name' => 'servers2',
'type' => \Zend\Form\Element\MultiCheckbox::class,
'attributes' => array(
'id' => 'servers2',
'class' => 'form-control',
),
'options' => array(
'label' => 'Servers 2',
'column-size' => 'sm-10',
'label_attributes' => array('class' => 'col-sm-2'),
'twb-layout' => 'horizontal',
'value_options' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
),
)
);

To specify the checked options, as seen at
use the 'selected' => true attribute:
$options = array(
array(
'value' => '0',
'label' => 'Apple',
'selected' => false,
'disabled' => false,
'attributes' => array(
'id' => 'apple_option',
'data-fruit' => 'apple',
),
'label_attributes' => array(
'id' => 'apple_label',
),
),
array(
'value' => '1',
'label' => 'Orange',
'selected' => true,
),
array(
'value' => '2',
'label' => 'Lemon',
),
);

Related

How to build a single 'flag' checkbox in prestashop with form helpers?

I can't figure out from official docs how to build a single checkbox element from the standard helpers. I already have the relevant boolean entity in database and I can build radios or selects as well for it, and they work.
But what I'd really like is to have a single checkbox to use as a boolean flag.
Anyone knows how?
Ok, the answer is to just use the 'switch' type: that will build a 'slider' switch on backoffice page. For future reference, I'm gonna report 3 different ways to accomplish the same task: radio, select and switch.
They have all been tested on AdminAddressesController and are bound to a custom DB boolean field called 'expo'.
//SELECT
$s_options = array(
array( 'expo' => 1, 'name' => 'Yes' ),
array( 'expo' => 0, 'name' => 'No' )
);
$temp_fields[] = array(
'type' => 'select',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'options' => array(
'query' => $s_options,
'id' => 'expo',
'name' => 'name'
)
);
//RADIO
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'radio',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => $s_options
);
//SWITCH
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'switch',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'is_bool' => true,
'values' => $s_options
);

Zend framework 2 how to add class to select input

How can I add a class to a select drop down list.
Below is my attempt to do it. I have tried to do it with the attributes but this doesn't appear to work with select tag.
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'jobId',
'options' => array(
'object_manager' => $this->getObjectManager(),
'target_class' => 'FormDependencies\Entity\JobList',
'property' => 'job',
'empty_option' => '--- please choose ---',
'attributes' => array(
'class' => 'testing',
)
),
)
);
Thank you in advance for your help.
below is the rendered input bar: you will see that the class has not been added;
<select name="Jobs[jobId]">
The attributes array should not be nested within options; but rather on the same level.
$this->add(array(
'name' => 'jobId',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'foo' => 'bar'
),
'attributes' => array(
'class' => 'testing',
),
));

Setting default checked value in ZendFramework2 to RADIO input (QuickStart form)

Unfortunately i don`t have the exact example here, but, is similar to this:
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender'
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
)
));
How can I set a default value to this element? I tried putting this, but didn't work:
'attributes' => array(
'value' => '0'
)
Thank's! And, sorry for my poor english! I need to improve it!
You should provide the value underoption, not attribute.
LIke this :
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender'
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
'value' => 10, // here
)
));
See this link:here
Radio is just an extension of MultiCheckbox
You will need to set the checked_value within the options array
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender', // <-- missing comma here also
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
'checked_value' => 10,
)
));
You can see this in the setOptions() method of Zend\Form\Element\Checkbox which the Radio element extends.
Your syntax is correct. value key in attributes is used to set the default checked value.
You just forgot to add a comma after 'name' => 'gender'
The following code will work for you
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender',
'attributes' => array(
'value' => '0',
),
'options' => array(
'label' => 'What is your gender?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
),
));

How to set a variable in ObjectSelect criteria params in DoctrineModule

I'd like to know how to set a variable in ObjectSelect criteria params.
My code is as follow:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'shop',
'attributes' => array(
'class' => 'chosen-select form-control'
),
'options' => array(
'object_manager' => $this->objectManager,
'target_class' => '\Godana\Entity\Shop',
'property' => 'name',
'label' => 'Shop',
'label_attributes' => array(
'class' => 'col-sm-3 control-label',
),
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('owner' => $this->shopOwner),
),
),
),
)
);
and it returns empty value but if I use a static value like 'criteria' => array('owner' => 1) it returns data from my db.
shouldn't it be $this->shopOwner->getId() ?

Zend_Dojo_Form passwordTextBox validator don't work

I am a newbie in zf, recently I m making a register form using zend_dojo_form and there are two passwordtextbox elements, which u know, one is for entering password and the other is to confirm the former one, then I using the validator 'token' but failed, here is part of my code.
$this->addElement('PasswordTextBox','password',array(
'label'=>'password:',
'required'=>true,
'trim'=>true,
'regExp'=>'^[a-z0-9]{6,18}$',
'invalidMessage'=>'password should be 6-18',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
//$this->addElement($password1);
$this->addElement('PasswordTextBox','password2',array(
'label'=>'confirm password:',
'required'=>true,
'trim'=>true,
//'regExp'=>'^[a-z0-9]{6,18}$',
'validators'=>array(array('identical',false,array('token'=>'password'))),
'invalidMessage'=>'the password you enter not the same',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
Here's a sample that should be functional:
class Application_Form_Register extends Zend_Dojo_Form {
public function init() {
$this
->setOptions(array('name' => 'registerForm'))
// username
->addElement('TextBox', 'first_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'name_register',
'required' => true,
'label' => 'Fornavn(e):',
))
->addElement('TextBox', 'last_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'surname_register',
'label' => 'Efternavn:',
))
->addElement('ValidationTextBox', 'email', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'EmailAddress',
array('StringLength', false, array(3, 60)),
),
'regExp' => "^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$",
'id' => 'email_register',
'required' => true,
'label' => 'E-mail:',
))
//password
->addElement('PasswordTextBox', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'regExp' => "^([a-zA-Z0-9_\-!-=]){4,32}$",
'tooltipPosition' => 'above',
'invalidMessage' => 'Krav til password: mellem 4 og 32 i længde og kun karakterer som tal, bogstaver eller blandt [ _-!"#%&/()=] er tilladt',
'id' => 'password_register',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register1',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register2',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('Hidden', 'action', array(
'id' => 'action_register',
'value' => 'register',
))
->addElement('Button', 'foo', array(
'id' => 'foo_register',
'onClick' => 'application.submitRegistration(dijit.byId(\'registerForm\'))',
'label' => 'Register'
)
);
$this->_decorate();
}
// renders nulled empty labels and adds classnames, related to element name
protected function _decorate() {
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form_contents')),
'DijitForm'
));
foreach ($this->getElements() as $el) {
$el->addDecorator('HtmlTag', array('tag' => 'div', 'class' => $el->getName() . '-wrap'));
if ($el->helper == 'Button')
continue;
else if (strlen(trim($el->getLabel())) == 0)
$el->addDecorator('Label', array('tag' => null));
else
$el->addDecorator('Label', array('tag' => 'div', 'class' => $el->getName() . '-label'));
}
}
}
Focusing on the two password fields - in terms of what makes clientside validation tick, lets filter out the Zend_Form_Element attributes:
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
// widget id
'id' => 'passwordconfirm_register1',
// both client-/server-side - triggers 'emptyMessage' validation
'required' => true,
// runs with the form.validate() and events onblur, oninit, onkeypress
// if ! dijit.get("value").match(dijit.regExp) show 'invalidMessage'
// this example matches the serverside reqs
'regExp' => "^([a-zA-Z0-9_\-!-=]){6,32}$",
// shown with form.validate(), dijit.validate() and input.blur() if regExp is not a match against value
'invalidMessage' => 'Password musts: 6 to 32 chars and only numbers, letters or amongst [ _-!"#%&/()=]',
))
And a second pwd field, same attributes but with a different validator
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
array('identical',false,array('token'=>'password'))
),
// widget id
'id' => 'passwordconfirm_register2',
'required' => true,
// in theory, we would define 'validator' in programmatic construct of
// the validationtextbox. but this will not work with the way Zend handles dojo
// 'validator' => 'function(value, constraints) { return "BAD PRACTICE" }'
));
// add javascript code to run onload and to extend the 'register2' properly
$this->getElement()
->getView()
->headScript()
->appendFile($baseUrl . '/js/Validator.js');
By now, only 'required' is validated clientside in the passwordconfirm_register2 widget. Lets fix in what markup factory does in regards to 'bad practice' in the above lines. Remove that line and add the javascript code instead, containing:
// file: baseUrl/js/Validator.js
dojo.addOnLoad(function() {
dijit.byId('passwordconfirm_register2').validate = function(value, constraints) {
if(dijit.byId('passwordconfirm_register1').get("value") != value) {
this.invalidMessage = "Passwords are not identical";
return false;
}
return true
});
});