Symfony Sonata project: How to add multiple input texts to block? - php

I would like to add a collection of input text with same name (i.e. name="blabla[]") filed to admin block with add/delete buttons.
I'm using collection form field type but can't see add/delete buttons
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('title', 'collection',
array('type' => 'text' ,
'required' => true,
'allow_add' => true,
'data' => array('First' => 'One')
)
)
)
));
}
I get below result without add/delete buttons!
Any idea how to get it working ?

I think you should use sonata_type_collection or sonata_type_native_collection instead of collection.
Here is an extract of the field doc :
14.1.7. SONATA_TYPE_NATIVE_COLLECTION (PREVIOUSLY COLLECTION)
This bundle handle the native Symfony collection form type by adding:
an add button if you set the allow_add option to true. a delete button
if you set the allow_delete option to true.

Related

Symfony2- get data of auto populated drop down

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

other in entity choice list form

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.

Select existing entity or create a new one

I've read Symfony 2 Entity field type with select and/or add new and this Symfony2 Form : Select an entity or add a new one but those topics didn't solve what I want.
I want for user to choose from existing entities but if he wants to, should be able to create a new one after clicking on a button ( which will render form ). I know how to render entities in select, I know how to render field which allows user to create few new entities ( collection ) but I don't know how to render both of them together.
right now I have select:
->add('place', 'entity', array(
'required' => false,
'class' => 'MyBundle\Entity\Place',
))
but as I said - I want to allow user to add new entity if he wants to. I was trying with collection:
->add('place', 'collection', array(
'required' => false,
'data' => [new Place()],
'type' => new \MyBundle\Form\Place\PlaceType(),
'allow_add' => true,
'options' => array( 'label' => false),
))
but that only allowed user to create new entities, not to select from existing ones...
If you want them to be able to add a new entity and select an existing one i'd suggest you take a look at form events.
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

Symfony2 Embed form values

I have my own utility CalculatorType for just calculating values for my document(I am using ODM), not reference for another document or sub arrays|object.
It has simple 2 inputs:
$builder
->add('price', 'text', array(
'label' => false,
'data' => isset($options['data']) ? $options['data']->getPrice() : '0.00'
))
->add('count', 'integer', array(
'label' => false,
'data' => isset($options['data']) ? $options['data']->getCount() : '10000'
));
In parent form I have:
$builder->
... // multiple fields
->add('calculator', 'calculator');
So when I am trying to save my form, I have an error:
Neither the property "calculator" nor one of the methods
To skip setting calculator field, I've added mapped => false to options
->add('calculator', 'calculator', array('mapped' => false));
and added eventlistener to transform calculator data
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$data["price"] = $data["calculator"]["price"];
$data["count"] = $data["calculator"]["count"];
unset($data["calculator"]);
$event->setData($data);
});
Now form submits values but calculator fields not passing to embed form, because of unsetting $data['calculator']
If I comment unset($data["calculator"]); then I have an error
This form should not contain extra fields
So I can't find any way to make this form work. Any ideas?
Found mistake in my form, so there is option for such type of forms: http://symfony.com/doc/current/cookbook/form/inherit_data_option.html

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