Symfony 2.8 how to default select value in Dropdown using Propel - symfony

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

Related

Default value of checkbox in 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();

Add roles in FOSUserBundle

I feel the same as a forum user who posted this:
I implemented FOSUserBundle, and I want to add to RegisrationFormType roles that are taken from a table. When I had it like this:
->add('roles', 'choice', array('label' => 'Rol', 'required' => true,
'choices' => array( 'ROLE_ADMIN' => 'ADMINISTRADOR','ROLE_SUPERADMIN' => 'SUPERADMINISTRADOR',
'ROLE_USER' => 'USUARIO'), 'multiple' => true))
And it works! But they must leave the BD, I can not put the Entity field because roles should be an array, not an object. How I can generate the array with the roles taken from a table? In FosUSerbundle as you would add roles?
Thanks ....
I write because that user had no answer. I followed [the steps of official documentation] (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md) and adding the above lines in the register of FOSUserBundle works, but I want to work this from the database.
And then I used to create groups this. Two additional tables were created and even now joined a group or role in the list, but not how to show the login to register a new user.
Has anyone solved it?
So you have the roles in a table? You could inject the EntityManager in the form type and use it to fetch the choices.
->add('roles', 'choice', array(
'label' => 'Rol',
'choices' => $this->getRoles(),
'multiple' => true,
'required' => true,
))
Then create a method which gives you the data the way you need.
Something like:
public function getRoles()
{
$roles = array();
$er = $this->em->getRepository('AppBundle:Role');
$results = $er->createQueryBuilder('r')
// conditions here
->getQuery()
->getResult();
// process the array?
foreach ($results as $role) {
$roles[$role->getId()] = $role->getLabel();
}
return $roles;
}

<select> in Symfony

How do I can create a in symfony form from the controller?
$form = $this->createFormBuilder($fupv)
->add('idUsuario', 'text')
->add('permiso', 'text')//I want a select here
->add('save', 'submit')
->getForm();
You need to use a choice Field Type.
There are various options depending on how you are populating the select.
A simple example;
$form = $this->createFormBuilder($fupv)->add('gender', 'choice', array(
'permiso' => array('a' => 'Admin', 'u' => 'User')
));
Have a look at the symfony docs for more examples.
You have to use the choice field type
->add('myField', 'choice', array(
'choices'=> array('choice1'=>'printedvalueofchoice1','choice2'=>'printedvalueofchoice2'),
'multiple'=> false,
'expanded'=> false ))
expanded set to true will turn your select into radio options

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']

Resources