Symfony2 classless form with choices: retrieving submitted data - php

I am having a problem using symfony2 and a classless form with a choice field.
The setup is very, very simple:
$data=array();
$choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing');
$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
'choices' => $choices,
'data' => $data,
'required' => false,
'expanded' => true,
'multiple' => true,
'mapped' => false))->
add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();
And then...
$request=$this->getRequest();
$form->handleRequest($request);
if($form->isValid())
{
//Go nuts.
}
It is in the "go nuts" part where I am stuck. I actually need to access the selected choices by their original index as given in the $choices array... And I haven't been able to. This is the closest I got:
$submitted_data=$form->get('mythings');
foreach($submitted_data as $key => $value)
{
echo $key.' -> '.$value->getData().'<br />';
}
Which assuming we mark the first and last choices, yields:
0 -> 1
1 ->
2 -> 1
It is absolutely necessary that this be a classless form. How do I access the original index (in our case, 1, 2 or 100) of the choices?.
Thanks a lot.

$form->get('mythings')->getData()
will give you data that you need.
I hope this helps.

It's an issue with field mapping. the data you're passing into the form needs to be applied to the field you're adding ('mythings') so you have to wrap $data in an array as a value to the key 'mythings'.
For example:
$data=array();
$choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing');
$fb=$this->createFormBuilder(array('mythings' => $data));
$fb->add('mythings', 'choice', array(
'choices' => $choices,
'required' => false,
'expanded' => true,
'multiple' => true))
->add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();

you could try naming the array keys to preserve the actual index
$data=array();
$choices=array('1' => 'A thing', '2' => 'This other thing', '100' => 'That other thing');
$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
'choices' => $choices,
'data' => $data,
'required' => false,
'expanded' => true,
'multiple' => true,
'mapped' => false))->
add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();

Related

One symfony form block is displayed by default, but I need zero blocks

I built a new form and by default there's one form-block displayed in the collection of blocks.
I want to have zero form-blocks in this collection by default.
What parameter is responsible for the number of form-blocks/entries in the collection by default?
I've attached the buildForm function for the collection in my ClassType.php file.
$builder
// [..]
->add(
'images',
CollectionType::class,
[
'entry_type' => StoAttachmentType::class,
'by_reference' => false,
'allow_add' => !$options['disabled'],
'allow_delete' => !$options['disabled'],
'prototype_name' => '__attachments__',
'disabled' => $options['disabled'],
'attr' => [
'class' => 'collection',
'data-prototype-name' => '__attachments__',
'data-add' => $options['disabled'] ? '0' : '1',
'data-allow-delete' => $options['disabled'] ? '0' : '1',
'data-links-template' => 'collection',
'data-file-preview' => '1',
'data-help' => 'Sto.images.help'
],
'label' => 'Sto.images',
'entry_options' => [
'disabled' => $options['disabled'],
'multiple' => true,
'withMain' => true
],
]
)
StoAttachmentType consist of three fields. The first one is required => True. Others fields is required => False.
Problem was resolved by overwriting class, but it's too much code. Is there any easier way?

symfony how get selected data from select

Entity Type is defined as follow in my form:
->add('esame_' . $i, EntityType::class, array(
'label' => false,
'mapped' => false,
'class' => 'AppBundle:Nome_esame',
'required' => true,
'multiple' => true,
'choice_label' => 'nome',
// 'disabled' => 'disabled',
'attr' => array(
'placeholder' => 'Esami',
'class' => 'max_width esame_row select_esame',
// 'class'=>'col-md-12 col-md-offset-0 col-xs-9 col-xs-offset-3 ',
)
))
In my form submit, I want to take all the selected value and create a new entity from every one.
I tried this:
foreach($form->get('esame_0')->getData() as $value){
$field= new Field();
$field->setvalue($value); // ->$value i want is the val of selected option
}
but $form->get('esame_0')->getData() does not return the selected data..
How can I do it?
The value that comes with EntityType is an Entity object. Try this way.
$entityObject = $form->get('esame_0')->getData()
$data = $entityObject->getId() or $entityObject->(Entity getter function)

Symfony2 Sonata Admin shows text after creating a new item

First, i'm sorry for my bad english,
im using sonata-admin bundle, and adminstrating my own Entity, when i'm creating a new item I have a block of text (the text that i write in the first input) that appears in the top of the Admin page, and it's ugly when its a big paragraph.
Lets say my first input is "Content", so when I write " This is my content " and save the item, it says "The item " This is my content" has been created succefully", how can I make another input be written in the success message
EDIT :
Here is a printscreen :
$formMapper
->add('page_mere1', 'choice', array('label' => 'Page mère 1', 'choices' => array('Podologie' => 'Podologie', 'Podz Pro'=>'Podz Pro')))
->add('page_mere2', 'choice', array('label' => 'Page mère 2', 'choices' => array('Pathologies' => 'Pathologies', 'Maladies spécifiques'=>'Maladies spécifiques')))
->add('page_mere3', 'choice', array('label' => 'Page mère 3', 'choices' => array('Pied' => 'Pied', 'Cheville'=>'Cheville', 'Jambe'=>'Jambe', 'Genou'=>'Genou', 'Hanche'=>'Hanche', 'Dos'=>'Dos')))
->add('translations', 'a2lix_translations_gedmo', array(
'translatable_class' => "Antipodes\SiteBundle\Entity\Pathologie",
'fields' => array(
'titre' => array(
'field_type' => null,
'required' => false,
'label' => 'Titre.',
'locale_options' => array(
'fr' => array(
'label' => 'Titre'
),
'en' => array(
'label' => 'Title'
)
)
),
'Definition' => array(
'field_type' => null,
'required' => false,
'label' => 'Definition.',
'locale_options' => array(
'fr' => array(
'label' => 'Definition'
),
'en' => array(
'label' => 'Definition'
)
)
),
)
))
;
//.......
Thanks in advance
I'm not sure if I'm understanding you correctly, but if you want to change the OK flashbag message after an successful edit you can do this with the _toString() method in your entity.
Open your Pathologie entity and add/edit the __toString() function, example:
public function __toString() {
return $this->titre;
}
The save-flashbag would then merge the "titre"-value instead of "Definition"-value into the message.

How to set default value for form fields- Magento?

I have an admin form field(textbox),
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
));
I need to set a default value for this text field. I tried, setting 'value' => '120', in it. But its not working.
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'value' => '120',
));
How to set a defalt value in that field?
In my case, setValues() function was overriding the 'value' that I had set for the field.
Cause:
In my _prepareForm() function, I set the value as below:
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'value' => '120',
));
At the end of _prepareForm() function, there was the following line of code which would reset the form values:
$form->setValues($model->getData());
Solution:
Set model data before setValues() function as below:
if (!$model->getId()) {
$model->setData('ajax_time_interval', '120');
}
$form->setValues($model->getData());
you can do that by adding "default" attribute in field configurations.
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'default' => '120',
));
Remove the last semicolon....
Check this site help-me a lot:
http://www.excellencemagentoblog.com/magento-admin-form-field
Gl mf

how to do hide and show elements in zend?

I had written the hide and show script for the two elements by checking checkbox in zend, but the value 1 will be sent to the function for both check and uncheck, So it only shows the element but it doesn't hides the element. how to resolve it ?
$this->addElement('checkbox', 'Change_Password',
array('decorators' => $this->elementDecoratorsTr ,'label' => 'Want to Set password ? :',
'required' => false,
'onchange' => 'passwordShow(this.value)',
'Options' => array(
'checkbx1' => 'checkbx1'),
));
$this->addElement('Password', 'User_Password',array(
'decorators' => $this->elementDecoratorsTr,
'label' => 'Password:',
'style' => 'display:none;',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(array(
'validator' => 'StringLength'))
));
$this->addElement('Password', 'Confirm_Password',array(
'decorators' => $this->elementDecoratorsTr,
'label' => 'Confirm Password:',
'style' => 'display:none;',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(array(
'validator' => 'StringLength'))
));
Here is my script function to hide and show the two elements User_Password and Confirm_Password
<script>
function passwordShow(password)
{
alert(password);
if(password)
{
$("#User_Password").show();
$("#Confirm_Password").show();
}
else
{
$("#User_Password").hide();
$("#Confirm_Password").hide();
}
}
</script>

Categories