I have a simple form to filter a list in function of themes :
$builder
->add('themes', EntityType::class, array(
'class' => 'XXBundle:Theme',
'choice_label' => 'image',
...
))
->add('save', SubmitType::class)
;
And in Twig I try to access all themes to do a specific render, in fact I would like to display Image (related to each theme : theme.image) instead of the theme.
I followed other solutions : Symfony2 + CreateFormBuilder how to render an image in a form
But it doesn't work :
form.vars.value.themes / form.vars.data.themes (or form.themes.vars.value / form.themes.vars.data this is the same)
exists but are always empty. Because this is a new form. If I submit the form, it works form.vars are not empty anymore.
How can I get themes when I display the form for the first time ? I followed the doc (http://symfony.com/doc/current/reference/forms/twig_reference.html#form-variables-reference) but I can't find what I want.
TY
Basically the EntityType is a List of choices - form.themes.vars.choices. Its an array of ChoiceView, to get entity simply access public data property.
So use this property to access to the entities:
form.themes.vars.choices
More accessible attribute are listed in the doc here.
Hope this help
Related
I am trying to add a checkbox in my form in Symfony.
The aim of that checkbox is to eventually remove uploaded image if checked.
So it shows up only when a file is uploaded. I also want it to cancel upload before form entity is persisted.
But I am not there yet, the code that is supposed to do that is currently commented out.
This checkbox corresponds to a boolean property in my File class so that it allows to access its value in controller. At the beginning that property didn't exist and so I was unable to access this value from entity or form /getData.
My problem is having the field in my formtype messes up with persistence.
All properties acquired from the form are considered null by doctrine. Even though a file was selected, renamed with uniqid etc . When time has come to flush, the fields are considered null. Removing DeleteFile from form solved the issue. I have tried setting it with property mapped = false, to no avail.
Here is code from form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file', array(
'label' => false
))
->add('deleteFile','checkbox', array(
'label' => 'Delete File',
'attr' => array('class' => 'delete_file'),
'mapped' => false,
))
;
}
Thank you for your input. I am also open to alternative solutions to that "interface" if a better solution can spare me from having to deal with this issue I am interested.
I have a form that contains a collection field-type.
However I don't know how to add/remove fields to/from the collection.
$builder
// ...
->add('covers', 'collection', array(
'required' => false,
'type' => new BookCoverType(),
'allow_add' => true,
))
;
The rendered form looks like this:
How can I add a new cover using the collection form field?
Symfony does not provide the add/remove JavaScript methods/buttonss or any session-based solution without JavaScript out of the box.
It renders a data-prototype attribute that can be used as described in the documentation chapter How to Embed a Collection of Forms -> Allowing "new" tags with the "prototype".
Some bundles provide this functionality though. Those are primarily the bootstrap bundles:
braincrafted/bootstrap-bundle
mopa/bootstrap-bundle
...
Just dive into their code - i.e. braincrafted/bootstrapbundle's bc-bootstrap-collection.js.
I would like to create a custom form field. For example, select field of cities in the world.
I read this article. In this article, the data are loaded using parameter file config.ynl. I, however, I'd like to upload this data from my database.
http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
Can somebody tell me how to do this or send the link to the example
In this case, better is to use an Entity Field instead of a Choice Field. It allows you to request your database to load your entities as follow,
$builder->add('MyField', 'entity', array(
'class' => 'MyBundle:MyEntity',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e');
},
'property' => 'something',
));
The property option define the entity property you want to use.
You can then customize your field using the "multiple" and "exanded" options to behave the way you want. Read the documentation
I have an order and a client entity.
I am wondering if it's possible with the actual Symfony2 form system to create an order form which will allow to:
Select several clients from a dropdown (mix of collection and entity form type)
And to create new clients on the fly (the default way for the collection type) if not in the dropdown list.
I've seen some way to do it by creating multiple forms in the same page, but this is not the way I would like to achieve it.
Are there any better ways to do this?
I had a similar problem which may lead to your resolution:
I have a Category and Item relationship (Many-to-One) and I wanted to either select an existing item or create a new item.
In my Form class:
$builder->add('item', 'entity', array(
'label' => 'Item',
'class' => 'ExampleItemBundle:Item',
));
$builder->add('itemNew', new EmbedItemForm(), array(
'required' => FALSE,
'mapped' => FALSE,
'property_path' => 'item',
));
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (!empty($data['itemNew']['name'])) {
$form->remove('item');
$form->add('itemNew', new EmbedItemForm(), array(
'required' => TRUE,
'mapped' => TRUE,
'property_path' => 'item',
));
}
});
You can map two fields in a form to the same property using the property_path option. Then, using form events, use the submitted data to make a decision and modify the form so that only one of the fields has a mapped option that is true.
If I have understood, you want to create and store new clients in a Form "on fly", at the moment. I think that you have to do that using JavaScript and set an additional action in your controller.
JS -> Capture the event to add new client to you database (i.e. "Add new" button click event)
JS -> Inside this event, call via AJAX to your controller with the values of new client. (Using FOSJsRoutingBundle is easy to do)
Symfony2 -> Inside your new action, store the new client in your database.
JS -> OnSuccess event, in your AJAX call, add the new Client to your DropDownBox
(ddb.append(new element tag)
Just doing that you have your new client stored in the database and added to your dropdownbox
For my part i had the same kind of problem and i resolved it by creating 2 attribute in my formType;
For example, for you it would be:
customer->entity
new_customer-> collection
In your order entity file you will have to add 3 methods (getter, setter, and remover) getter and remover don't do anything but setter should call the setCustomer(c)
I'm not sure if it is the best way but it's the only way I figure it out!
The collection Form type allows to add and delete on the fly with allow_add and allow_delete attribute.
More informations by following these 2 links :
Official collection form field type reference
Cookbook about add and delete on the fly with collection type
If you don't like to get supplementary forms on the same page, you can integrate them in dialog boxes... But you definitely need a form to create new items...
I want to prefix the form title within the form tags, but above the form elements.
<form>
<h3>Login Form</h3>
<!-- form elements -->
</form>
I do not want to use a view script.
I thought of using the Description decorator, but that appears to be only available to the elements.
How do I set an arbitrary description for a form, and position it (either append / prepend) within the form tags? I have a feeling I need to play with the HtmlTag decorator but I've played with it and can't get the right results.
Typical. Ask a question, work out the solution.
$form->setDecorators(array(
array(
'Description',
array(
'tag' => 'div',
'class' =>'title'
)
),
'FormElements',
'Form'
))
->setDescription('Enter Login Credentials:');
Depending where you place the description decorator will determine where in the form tags it displays, i.e. place above 'FormElements' to display above the elements; place below 'FormElements' to display the description below the elements; place below the 'Form' decorator to display after the form tags.