Symfony2- get data of auto populated drop down - php

I'm having two dropdowns which are not dependent on any entity. When I select the value of first one an ajax function is called to populate the second one which is done perfectly. But, the form after submission, always returns
type_name: ERROR: This value is not valid
My form looks like:
->add('type', 'choice', array(
'empty_value' => "Select",
'choices' => array(
1 => 'One',
2 => 'Two'
)
))
->add('type_name', 'choice', array(
'empty_value' => "Select",
'choices' => array(
),
))

This is because Symfony needs to know the choices beforehand in order to validate them correctly. Currently you are indicating that no choices are valid for your dropdown type_name as you are passing an empty array as choices.
Please check this answer for a detailed guide on how to implement dynamic choices and choice validation: https://stackoverflow.com/a/13374841/606104

Related

cakephp3 form radio control access other entity value

I need to populate some entity value into the radio HTML code, I look
through all documentation but does not seem to be able to find any help
below is the php code, {{entity->description}} is the entity value I want to show
$this->Form->setTemplates ( [
'radioContainer' => '{{content}}'
] );
echo $this->Form->control ( 'OrderShippingMethodId',
[
'templates' => [
'nestingLabel' => '**<p>{{entity->description}}</p>**
{{input}}{{text}}',
'radioWrapper' => '{{label}}' ],
'options' => $shippingMethodsRadio,
'type' => 'radio',
'label' => false,
'type' => 'radio',
'id' => 'OrderShippingMethodId',
'legend' => false ,
'escape' => false
] );
it generate the below html code, the <p></p> is not showing the entity description value
Shipping
as can be seen in the bold code above, the entity->description value cannot be read
out, seem that the only value the form->control can read out from the
entity is text, value. is there anyway to access other value in the entity
in the controller method, to populate $shippingMethodsRadio
$this->set('shippingMethodsRadio' => $shippingMethods->extract('radio'));
the $shippingMethods, will contain a collection of the entity shippingMethod, I want to read other value like description property in the entity into the radio control, the radio property in the shippingMethod return an array contains value, text, description property of the entity to populate the radio control
thank you

Symfony : How to create Multiple RadioBoxes using the form Builder

I'm working on a new project where i was asked to create this type of form :
I have created an entity with a json_array attribute that stores this form
configuration.
example :
{
"DescenteCde" : 0 // checked radiobutton is "Aucun"
"WebShopPC" : 1 // checked radiobutton is "Faible"
}
I want to know the easiest way to generate this type of form using the symfony form builder.
I found a solution for my problem. Actually, Symfony framework provides us with A Collection Type that takes a array as a parameter and then we specify the different options and values/
$ImpactApplicationFormBuilder->add('***Configuration***',CollectionType::class, array(
'entry_type' => ChoiceType::class,
'entry_options' => array(
'choices' => array(
'Aucun' => 0,
'Faible' => 1,
'Moyen' => 2,
'Fort'=> 3
),
'multiple' => false,
"expanded" => true ,
),
));
Configuration attibute is of type array so in the form the different keys of your array will be rendered with the given options you provided.
Your result will look like this :
Hope this help anybody with the same problem

How to transform the tab of selected values in a select(html) into a field in symfony2

i have to work on a project with symfony2.
I have to create a form with many select input. These select inputs can have more one value selected.
I want to insert all the selected values in one field like that :
value 1
value 2
value 3
result => value1;value2;value3
So I don't have to have a one-to-many relation.
How can I do that, knowing each select input is generated thanks to their entity ?
and when I want to submit i have this error :
Warning: oci_bind_by_name(): Invalid variable used for bind in [...]/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php line 156
->add('cod_rom','entity',array(
'label' => 'Codes romes :',
'class' => 'xxxxListeBundle:CodeRome',
'expanded' => false,
'multiple' => true,
'required' => false,
'property' => 'libelle'
))

Symfony2 Form validation dependency between form_types in one form

In my symfony2 project I created a new FormType which is called "ChoiceAndOrTextType" which is a list of choices (checkboxes) but the user can also check the "others" option and write his answer into a textfield.
The code is like this one here in the answer of bschussek:
Use a conditional statement when creating a form
$builder
->add('choice', 'choice', array(
'choices' => $options['choices'] + array('Other' => 'Other'),
'required' => false,
))
->add('text', 'text', array(
'required' => false,
))
->addModelTransformer(new ValueToChoiceOrTextTransformer($options['choices']))
;
Now i want to validate this correctly, so when the user checks "Others" then the textfield needs to be filled out, if "Others" isn't checked it can be blank. (Kind of dependent validation).
How do I do this?
You need to use two validation constraints, for example in validation.yml yaml file

cakePHP assign selected values from 2 arrays using the html input helper

I create 2 arrays from a model, 1 being already selected values from the user, and the 2nd available values which the user can then also select. This is for an edit page. I want to populate a multi-select input box with the values of both models, but want the already chosen values (1st array) highlighted. It creates the models fine, and using array_merge() I merge both the arrays as the options, but the selected does not highlight the correct fields. Any tips?
// Controller:
$ivrNumbersAvail = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array($id)))));
$ivrNumbersSelected = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array(0)))));
// In the view:
echo $this->Form->input('SurveyIvrNumbers.id',array(
'empty' => '-- Select IVR Number(s) --',
'options' => array_merge($ivrNumbersAvail,$ivrNumbersSelected),
'selected' => $ivrNumbersSelected,
'class' => 'textbox',
'multiple' => true,
'div' => array(
'class' => 'field'
),
'label' => array(
'class' => 'label-tooltip',
'title' => '', //tool tips
'text' => 'IVR Numbers: (you can select multiple numbers)'
),
'after' => '<p class="field-help"></p>'
));
If you set $this->request->data to the record you are currently editing CakePHP will automatically populate this data for you!
// CONTROLLER
// this line sets the data
$this->request->data = $this->Survey->read(null, $id);
// this passes the SurveyIvrNumbers to the view, (you can put any options on to this)
$this->set('SurveyIvrNumber',$this->Survey->SurveyIvrNumber->find('all'));
// VIEW
// CakePHP does the rest
echo $this->Form->input('SurveyIvrNumbers',array(
'empty' => '-- Select IVR Number(s) --', // plus other options
);

Categories