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

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',
),
),
));

Related

How to put a picture instead of text in radio element in ZF2?

Subject: How to put a picture instead of text in radio element in ZF2?
code:
Options field:
array(
'spec' => array(
'type' => 'radio',
'name' => 'fcaptcha',
'options' => array(
'label' => 'Капча',
'label_attributes' => array(
'class' => 'control-label',
),
),
'attributes' => array(
'required' => 'required',
),
),
)
Controller:
$temp[0] = 'Here you need to put a picture instead of text';
$temp[1] = 'Here you need to put a picture instead of text';
$form->get('fcaptcha')->setValueOptions($temp);
You can do that just with some CSS. Here's a simple example that you can adapt for your purpose :
Let say you have this Zend\Form\Element\Radio element :
array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'fcaptcha',
'options' => array(
'value_options' => array(
'0' => 'something1',
'1' => 'something2',
),
),
'attributes' => array(
'id'=>"fcaptcha-id"
)
)
CSS :
radio-option1 {background-image:url(option1.png);}
radio-option2 {background-image:url(option2.png);}
Some JS :
$(document).ready(function(){
$(':radio[value="0"]').addClass('radio-option1');
$(':radio[value="1"]').addClass('radio-option2');
});

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',
),
));

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() ?

how to change the size property of a form text input?

1) I didn't find anything in CSS files in relation with the width of form input.
2) I tried this :
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
'size' => '14', <---this doesn't work!
),
'type' => 'Text',
),
),
without any result!
So, is it possible to change the width (size property) of a form text input in ZF2 ?
Thanks
Put size in attributes instead of options
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'size' => 14,
),
'type' => 'Text',
),
),
Thank you for your help but this way doesn't work for me.
Here is exactly my code :
$this->add(array(
'name' => 'name',
'attributes' =>array(
'type' => 'text',
'size' => '7', <--- this doesn't work
'maxlength'=> '7', <--- this works
'value' => 'Doe' <--- this works
),
'options' => array(
'label' => 'Your name',
),
));
However, "size", "maxlength" and "value" are equivalent : both are HTML attributes!
So I found in public/css/bootstrap.min.css :
input, textarea, .uneditable-input
{
width: 206px; <---change this value and it's ok
}
!!! Pay attention to refresh your browser !!!

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

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',
),
);