Symfony set value checked on a form type choice - forms

I create a form with FormBuilder with Symfony like :
$builder
->add('timeBarOpen', 'time', array('label' => 'Ouverture Bar', 'attr' => array('class' => 'form-control')))
->add('timeBarClose', 'time', array('label' => 'Fermeture Bar', 'attr' => array('class' => 'form-control')))
->add('timeStartHappyHour', 'time', array('label' => 'Début Happy Hour *', 'attr' => array('class' => 'form-control')))
->add('timeEndHappyHour', 'time', array('label' => 'Fin Happy Hour *', 'attr' => array('class' => 'form-control')))
->add('day', 'choice', [
'choices' => $days,
'multiple' => true,
'expanded' => true,
'label' => 'Jour(s) *',
])
;
$days is an array :
$days = array(
'Monday' => 'Lundi',
'Tuesday' => 'Mardi',
'Wednesday' => 'Mercredi',
'Thursday' => 'Jeudi',
'Friday' => 'Vendredi',
'Saturday' => 'Samedi',
'Sunday' => 'Dimanche',
);
So, this field type "choice" generates multiple checkboxes and I need them all to be checked by defaut when the form is created.
How can I do that?

You can use the data parameters to specify some default choices, in your case specify an array, and use the keys of your available choices
$builder
->add('day', 'choice', [
'choices' => $days,
'multiple' => true,
'expanded' => true,
'label' => 'Jour(s) *',
'data' => array_keys($days)
])
;

I had a similar problem with a ChoiceType drop-down list, where I wanted to be able to set the selected value, but I couldn't figure out how to do that. I figured it out from #ThomasPiard 's answer. Thank you!
In my example, I set the 'choices', and the 'data' is set to the value of the array (not the key). This is important - since I couldn't figure out why it didn't work at first.
Here's my sample:
->add('pet_type', ChoiceType::class, array( // Select Pet Type.
'choices' => array(
'Substitution' => 'sub',
'Equivalency' => 'equiv',
),
'label' => 'Select Petition Type:',
'attr' => array(
'onchange' => 'changedPetType()',
),
'placeholder' => 'Choose an option',
'data' => 'equiv',
))
Hopefully it will help someone with the same problem.

Related

Doctrine ChoiceType setting a default value on form load

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

How do I limit number of rows in the admin form in magento

I have following code in my Form.php
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
));
I want to restrict the number of rows to display it like a single row, the code: 'rows' => 1 doesn't work here.
You can use styling to do this
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
'style' => "height: 1em;",
));
Just had to change 'textarea' to 'text'

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

language settings in Zend Framework 2 new DateSelect/MonthSelect Form

i have quick questions: i need to set the month dates on my form to English settings; they are curretnly set in Romanian Langugauge
i followed the example set by bakura10 / gist:3705417 and set my dates as such:
$this->add(array(
'type' => 'Zend\Form\Element\DateSelect',
'name' => 'birthDate',
'options' => array(
'label' => 'Date',
'create_empty_option' => true,
'day_attributes' => array(
'data-placeholder' => 'Day',
'style' => 'width: 20%',
),
'month_attributes' => array(
'data-placeholder' => 'Month',
'style' => 'width: 20%',
),
'year_attributes' => array(
'data-placeholder' => 'Year',
'style' => 'width: 20%',
)
)
));
i then set my view helper as such;
echo $this->formDateSelect($this->form->get('birthDate'), IntlDateFormatter::LONG, 'ro_RO');
the problem is that this obviously gives me romanian setting; i dont know where to get the settings for English; i tried this but it does not work;
echo $this->formDateSelect($this->form->get('birthDate'), IntlDateFormatter::LONG, 'en');

Add custom form in magento admin side

I want to make custom form in magento with two section like (two green box)
Also I wand to make three fields instead of one like
I have make code like
protected function _prepareForm() {
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('slider_form', array('legend' => Mage::helper('slider')->__('Slider information')));
$fieldset->addField('link_title', 'text', array(
'label' => Mage::helper('slider')->__('Link Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'link_title',
));
$fieldset->addField('link', 'text', array(
'label' => Mage::helper('slider')->__('Link'),
'class' => 'required-entry',
'required' => true,
'name' => 'link',
));
$fieldset->addField('content', 'text', array(
'label' => Mage::helper('slider')->__('Text'),
'class' => 'required-entry',
'required' => true,
'name' => 'content',
));
....
....
}
Can anybody help!!!