other in entity choice list form - php

I'm new to symfony. I have a drop down in a form with data fetched from DB.
$builder->add('category', 'entity', array(
'label' => 'category',
'class' => 'MyBundle:category',
'expanded' => false,
'multiple' => false,
'mapped' => false,
'empty_value' => 'category'
));
$builder->add('other_category', 'text', array(
'label' => 'category',
'required' => false,
'invalid_message' => 'Please enter a valid category',
'mapped' => false,
));
the user can also add new category to the table. when other is selected from drop down, the 'other_category' input field is shown, else its hidden.
'Other' was added to drop down with the help of this code.
public function finishView(FormView $view, FormInterface $form, array $options)
{
$new_choice = new ChoiceView(array(), 'other', 'Other');
$view->children['category']->vars['choices'][] = $new_choice;
}
If a option is selected from drop down the form works fine. Data gets stored without any error. But if user selects 'other' and enters a new category the page reloads with 'This value is no valid' under the category options and there is no form validation for the 'other_category' entered by user.
Can someone help me with the form validation and also entering of a new category or suggest a better way to implement the above functionality.

The validation error is happening because the form field type is Entity, but there is no "MyBundle:category" entity with the identifying value "other".
You've not specified the "choice_label" property in your Entity form type so I'll assume your "MyBundle:category" entity has a __toString() function. This would mean none of the "MyBundle:category" entities return "other" in their __toString() function.
I can think of two options to work around this right now:
1) Add a "MyBundle:category" entity with value "other". This is the easiest way, but it's fairly assumed you don't want such a category to exist in your database.
2) Load the list of "MyBundle:category" entities in advance from your controller, build them into an associative array, append your "other" option to the array, then pass that array to the form. You'd need to swap the Entity form type for a Choice type and use the categories array as the choices.
If your form is a FormType class you'll need to pass the array in with the class constructor.
If you don't mind having a new category with an "other" value in your category table, just add it. Otherwise go for option 2, which won't make much difference to what you do after receiving the submitted form as this form field isn't mapped to an entity property anyway.

Related

How to use unmapped field in Symfony form to adjust data post submit?

I have two fields in my entity contractLength and contractEndDate. The aim is to be able to specify EITHER a contractLength OR a contractEndDate.
To accomplish this I have added an unmapped field to the form useBespokeEndDate, of checkbox type.
I want to be able in the form class ContractType to listen for the submit event and then check if the checkbox is ticked, if it is then proceed as normal but if it is not ticked (so we are using contractLength) then set the contractEndDate field to null.
I believe I should be using Event Listeners as detailed here http://symfony.com/doc/master/form/dynamic_form_modification.html#customizing-your-form-based-on-the-underlying-data (under the Dynamic Generation for Submitted Forms heading) to listen for the POST_SUBMIT event and then check the value of useBespokeEndDate and then modify the data as appropriate. However, I'm confused as to how exactly to get the form data for the unmapped field and then to also modify the actual data of the form.
My buildForm method in ContractType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('useBespokeEndDate', CheckboxType::class, array(
'label' => 'Use Bespoke Contract End Date?',
'empty_data' => false,
'mapped' => false
))
->add('contractLength', ChoiceType::class, array(
'choices' => array(12 => 12, 24 => 24, 36 => 36, 48 => 48, 60 => 60),
'label' => 'Contract Length (months)'
))
->add('contractEndDate', DateType::class, array('label' => 'Contract End Date'))
->add('save', SubmitType::class, array('label' => 'Save'));
$builder->addEventListener(FormEvents::POST_SUBMIT,
// Do something here to modify the data
);
}
Had the same problem, and I could access the unmapped values this way :
// in your POST_SUBMIT callback :
$form = $event->getForm();
$contract = $event->getData();
// get the unmapped value :
$useBespokeEndDate = $form->get('useBespokeEndDate')->getData();
// .. continue
I've got a similar situation where I have an option field, it's use (and therefore value) depending on a radio button group.
Rather than making a dynamic form, I instead separated the form from the entity, and actually ended up created a sub-form (Type) that had its own data_class Form configuration option and Data Mappers to convert the value objects to the entities.
There are useful blog posts with more information about Value Objects in forms and Creating a Custom Data Mapper for Symfony Forms.
For me, this alos gives the advantage of moving some quite complex checks into their own smaller class, and making the code more reusable.

Symfony 2.3: Remember form inputs while visiting another site

What I have:
A form with several different input fields etc. The interesting part is a collection field:
$builder->add( 'publicationAuthors', 'collection', array (
'type' => new AuthorPublicationType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'Autoren'
));
This collection refers to another FormType, including only an entity field and an order id:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add( 'author', 'entity', array (
'class' => 'indPubBundle:Author',
'multiple' => false,
'label' => 'Autor',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder( 'a' )
->orderBy( 'a.author_surname', 'ASC' );
}
$builder->add( 'order_id', 'hidden');
));
}
Via JS I am able to add new collection form fields to my form so the user can then choose several of the authors and order them.
What I need:
So far, the user can only choose existing authors due to the entity field. I want to enhance the form such that the user is also able to create new authors and use these in the form. I thought of adding a button which redirects the user to a new form where he can create a new author. Then, after submitting that form, the user should be redirected back to the original form and continue to fill it out.
The main problem here is, that I want to remember the user's previous inputs to the original form so that he doesn't need to start all over. Is there a way to remember these inputs while the user is using the author creation form?
I thought of remembering the data in the session, but there is a problem with that: If the user would open a new tab, he might overwrite the old form inputs, since the session ID is the same for both tabs.
Okay I found a solution that is satisfying for me.
Basically, I take the whole form data, serialize it and remember that in a hidden form field.
Because the serialize function returns several signs that are not useable in html, I also encode the string with base64.

How to add dynamically-created sub-forms as a collection in Symfony/Silex FormBuilder?

I'm trying to dynamically generate a form based on user-provided field definitions. This is fairly straightforward for the basic form:
$builder = $app['form.factory']->createBuilder('form', $my_data);
foreach ($my_user_provided_field_definitions as $field) {
$builder->add($field->handle, $field->type, $field->options);
}
$form = $builder->getForm();
But I also want to have sub-forms using the collection field type, and while it is so easy to dynamically generate the top-level form as I've done above, it does not seem possible to dynamically generate a sub-form for the collection field because all of the docs and sample code I've come across utilize either a single field like so:
$builder->add('my field', 'collection', array(
'type' => 'text',
'allow_add' => true,
));
...or a custom "Type" object, like so:
$builer->add('my field', 'collection', array(
'type' => new TagType(),
'allow_add' => true,
));
The TagType class in the above example must be defined in code (see http://symfony.com/doc/master/cookbook/form/form_collections.html )... but my problem is that I cannot have the class defined in code because these sub-forms are dynamically generated based on user data -- so I do not know what fields the sub-form will contain until run-time!
Is there a way to dynamically generate the sub-forms that can then be passed in to the top-level form, or is this a use case that is not handled by the Symfony FormBuilder?

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

Symfony 2 Create a entity form field with 2 properties

I am using symfony2 and have a form to save the relation of one user to some rules. These rules are set by the admin user of the company. In this form, after I selected a user to update, I have to select which rule this user have permission.
The problem is that I may have more then one rule with the same name (it's another entity) but the values are different. So, when I build the selectbox I must show the name and the value like:
Quantity of items - 10
Quantity of items - 20
Value of the item - 200
Value of the item - 500
But now I just can show without the "- $value" using the code bellow:
$form = $this->createFormBuilder()->add('myinput', 'entity', array(
'class' => 'myBundle:Rule',
'property' => 'childEntity.name',
'label' => 'Filas Permitidas',
'expanded' => false,
'multiple' => true,
'choices' => $this->getDoctrine()
->getRepository('MyBundle:Rule')
->findAll(),
'required' => true,
))->getForm();
So, as property I wanted to get $myEntity->getChildEntity()->getName() and the $myEntity->getValue().
Is there some way to do this?
Yes, define a getUniqueName() method in the entity class like:
public function getUniqueName()
{
return sprintf('%s - %s', $this->name, $this->value);
}
And edit the property form option:
'property' => 'childEntity.uniqueName',
You also can omit the property option and define the __toString() method same way in order to not repeat the setting of the property option in every form.

Categories