How to add default option in dropdown in symfony2? - forms

I'm creating a form date field which display day,month in drop down format. My problem is how can i add an option in it who's value is 0, so that when user dont want to select date 0 is stored in DB.
->add('monthPaymentDate', 'date', array(
'widget' => 'choice',
'empty_value' => array('year' => false, 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'required' => true,
))

You can do this in your entity:
public function setMonthPaymentDate($monthPaymentDate){
if (!$monthPaymentDate instanceof Datetime){
$date = 0;
}
$this->monthPaymentDate = $monthPaymentDate;
}

You can achieve that by adding placeholder to the field.
->add('monthPaymentDate', 'date', array(
'widget' => 'choice',
'empty_value' => array('year' => false, 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'required' => true,
'placeholder'=>'Choose a date'
))
Then the default value will be always null.

Related

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

Symfony set value checked on a form type choice

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.

cakephp birthday helper customize the default values

I would like the to add "month", "day", "year" as the first choice in the birthday menu helper? Can this be done? I don't see any examples of how to do this? Below is the code I have:
<?php echo $this->Form->input('date_of_birth',
array(
'type' => 'date',
'label' => 'Date of Birth:<span>*</span>',
'dateFormat' => 'MDY',
'empty' => true,
'minYear' => date('Y')-130,
'maxYear' => date('Y'),
'options' => array('1','2')
)
);
?>
Thanks,
Bart
Here you go, the empty option accepts an array where you can specify the empty value for the individual fields using the keys month, year, day, hour, minute and meridian:
echo $this->Form->input('date_of_birth',
array(
'type' => 'date',
'label' => 'Date of Birth:<span>*</span>',
'dateFormat' => 'MDY',
'empty' => array(
'month' => 'Month',
'day' => 'Day',
'year' => 'Year'
),
'minYear' => date('Y')-130,
'maxYear' => date('Y'),
'options' => array('1','2')
)
);