Default value of checkbox in Symfony - symfony

If the checkbox is selected, does the variable take the value 1, and if not selected the value 0?
$builder
->add('happy', 'checkbox', array(
'label' => 'Are you happy?',
'data' => false,
));
In this link, it is not clear to me:
The value that's actually used as the value for the checkbox or radio button. This does not affect the value that's set on your object.
https://symfony.com/doc/current/reference/forms/types/checkbox.html
Thanks for the answers!

Try this,
$builder
->add('happy', 'checkbox', array(
'label'=>'Are you happy?',
'data'=>false,
'attr' => array('checked' => 'checked', 'value' => '1')
));

You mean the return value on form submission? Going by the linked page, it looks like it should return a boolean:
if the box is checked -> the field will be set to true;
if the box is unchecked -> the value will be set to false.
To be sure, you could always try dumping out the result of
$form->getData();

Related

How to check whether checkox is checked in wordpress customizer

i am building a plugin and i want to add some settings which rely on checkboxes. if the user checks the checkbox how to know. i know how to get data from checkbox in php but got stuck in wordpress. Because we have no option to add name attribute to the checkbox. for example, i am adding a checkbox in customizer.
$wp_customize->add_setting('ion', array(
'sanitize_callback' => 'ion_checkbox'
));
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'ion', array(
'label' => 'Check',
'type' => 'checkbox',
'section' => 'search_submit_section',
'settings' => 'ion'
)));
As we see there is no option to add name attribute how we will know whether user has checked the checkbox or not. I hope you understand my question.
Thanks in advance
with ($wp_customize, 'ion', array.... the ID is ion you can get the value with
$ion_checkbox_checked = get_theme_mod('ion') ? true : false;
if the checkbox checked get_theme_mod('ion') will return 1 else 0

Symfony 2.8 how to default select value in Dropdown using Propel

I have created one dropdown for employee Role. In edit mode how to select user selected role.
$builder->add('homePageId', \Propel\Bundle\PropelBundle\Form\Type\ModelType::class , array(
'class' => 'Admin\HomePageBundle\Model\HomePage',
'required' => true,
'multiple' => false,
'expanded' => false,
'query' => HomePageQuery::create()->orderByName(),
'property' => 'name',
'preferred_choices' => array('5')
));
I have added "preferred_choices" for select "5" number role but not working.
Please help for fixed this issue.
The selected value is the object value. I'll try to explain this way:
$object = new Object();
$object->setHomePageId(5);
// kinda put the 5 as selected in your form, because your object contains 5 and the form is not binded yet.
$form = $this->createForm(ObjectType, $object);
$form->getData()->getHomePageId(); // will return 5
//kinda put the request parameter homepageId as selected in your form
$form->handleRequest($request);
$form->getData()->getHomePageId(); // will return the user selection

correct method to create drupal views field handler

I mean to have custom date handler in views ,according to documentation of views i should implement hook_views_api and hook_views_data .
my pseudo
function mymodule_views_api() {
return array(
'api' => views_api_version(),
);
}
and in hook_views_data()
function mymodule_views_data() {
$data = array();
$data['node']['created'] = array(
'group' => t('Mul2'),
'title' => t('Post date'), // The item it appears as on the UI,
'help' => t('The date the content was posted.'), // The help that appears on the UI,
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
return $data;
}
It's ok and create a views field gorup (Mul2),
I set it hanlder date handler for test, but it not work correctly and just show Mul2: Array , and broken/missing handler in configuration of it.
I try successfully get data of custom table with views data .Is it correct to set handler for a field before handlered (like created in node ) ?
any solution?any idea?
Method of Impelementation is correct, my mistake was in use a field that before set handler for it $data['node']['created'] .
if you want to change default handler of handled field you have use hook_views_data_alter(&$data) instead of try to set handler for it again!!!(Mul2:array because of you try to set handler for handled field).

Drupal 7 : Getting the values from radio buttons form api

So this is my form :
$active = array(0 => t('Poster'), 1 => t('Postcard'), 2=>t('Post it'));
$form['radioimage']['active'] = array(
'#type' => 'radios',
'#default_value' => isset($node->active) ? $node->active : 1,
'#options' => $active,
);
I want to know which radio button was selected. I am trying to access the data but I don't know what its called I can't even use devel for some reason.
I tried below but they all failed
$form_state['values']['radioimage']['active'][0]
$form_state['values']['radioimage']['active']
Drupal flattens the values in the $form_state array by default so
$form['radioimage']['active']
will actually come out in
$form_state['values']['active']
If you want to explicitly keep your naming hierarchy then you should set the #tree key on the parent element:
$form['radioimage'] = array(
'#type' => 'container',
'#tree' => TRUE
);
In that case the value will be in
$form_state['values']['radioimage']['active']

How to override default values of form on submit event

I have a form which has textfields with default values specified. On submit event, I want these default values to be changed with the new set of values that I am passing. I am using form_set_value($element, $value, $form_state) for this. However it is not updating. Any ideas? My code is
function sample_myform($form_state){
$form['field']['name'] = array(
'#type' => 'textfield',
'#title'=> 'Name: ',
'#maxlength'=> 127,
'#default_value' => param1,
);
$form['field']['placeholder'] = array(
'#type'=> 'value',
'#value' => array(),
);
$form['field']['button1'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function sample_myform_validate($form,&$form_state){
$name2 = $form_state['values']['name'];
form_set_value($form['field']['placeholder'], $name2, $form_state); */
form_set_value($form['field']['name'],'God',$form_state);
$form_state['rebuild'] = true;
}
One thing for sure, $form['field']['placeholder'] will never, ever change since you set #value. Once #value is set, Form API moves on. Be careful though with setting just #default_value on a #type value as that can be tampered with. You can do something like $form_state['placeholder'] = $name2; in validate and use that in the form builder function.
What you try to do with name works in Drupal 7 but I suspect you are in Drupal 6. The validate function overwrites $form_state['values'] just fine but that's not persisted for the form rebuild. Once again, save into $form_state as you need.

Resources