I have an entity with a many-to-many association.
What I'm trying to do is:
create the form for the main entity
embed the associated entities
I have been able to do both the points above, but now I cannot figure out how to retrieve an associated entity field.
My classes are Gallery and Immagine. Immagine has 3 properties, image, imageName and file.
Inside my GalleryType buildForm function I do this:
->add('immagini', 'entity', array(
'class' => 'MySiteBundle:Immagine',
'property' => 'image',
'multiple' => true,
'expanded' => true
))
Now, in my edit.html.twig template, if I write this
{% for img in edit_form.immagini %}
<li>
{{ form_widget(img, {'attr': {'style': 'vertical-align: top'}} ) }}
{{ form_label(img, null, { 'label_attr': {'style': 'display: inline'}} ) }}
</li>
{% endfor %}
...and the result of embedded images inside a gallery edit page, is correct and works great.
Now, what I want to do is to display in the same li tag, another Immagine field, let's say imageName.
I've tryed so many ways, but I'm not able to do it...
Any suggestion?
You should embed your associated entity by using collection type, not entity type like you did.
->add('immagini','collection',array('type'=>new ImageType(),'multiple'=>...,))
So, you need to implement a new form class, call it what you like eg. ImageType(). In that class type add whatever form types(properties) you would need to display, eg.
->add('imageName','text')
->add(...
Than you would be able to iterate and display it from twig code.
Take a look at basic form embedding docs:
http://symfony.com/doc/current/cookbook/form/form_collections.html
Related
I am trying EasyAdmin, and I am completely lost.
I have entity A which contains a relationship with entity B (which contains multiple fields).
In Entity A's CrudController, if I want to display fields from the relation (so, Entity B), how am I supposed to go about it?
In my current case I have a Portfolio entity that has a relationship with UserDetails (and which contains several fields that I therefore want to display in the form of the Portfolio entity)
I created a FormType for my UserDetails class, as we normally would. And in my CRUD controller, for the Portfolio entity, I put this:
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id'),
FormField::addPanel()->setProperty('userDetails')->setFormType(ProjectUserDetailsType::class)
];
}
Is this the right way to go? Will the fields contained in this FormField be mapped correctly?
On the other hand when rendering, this "subform" is centered, unlike the others.
When you try to embed a single object or form you could follow this doc
If you want also embed a collection of objects there is a way for that using what is called prototypes.
For rendering the form and styles you could render each part separately and adding the proper styles on twig files or inside the form definition.
#rendering with twig
{{ form_label(form.field, 'label', { 'attr': {'class': 'foo'} }) }}
{{ form_widget(form.field, { 'attr': {'class': 'bar'} }) }}
#rendering inside html tags
<div class="form-control">
<i class="fa fa-calendar"></i> {{ form_label(form.dueDate) }}
{{ form_widget(form.dueDate) }}
<small>{{ form_help(form.dueDate) }}</small>
<div class="form-error">
{{ form_errors(form.dueDate) }}
</div>
I have a many many to many relation in Sonata (two one to many relations to be exact), Brand and Retailer.
In the Brand admin Edit page, I want to display all the retailers as a list (so just a read only version), instead of having the normal edit (at the moment, on this brand edit page, I can manage the relationship between this brand and retailers - add a new one, delete an existing one).
I tried to explore two routes so far:
Edit page will load a custom twig
Using a custom field type for this field only
My issue is, with both options, I didn't manage to get to a solution
So here is what I have done:
1 - Loading a custom edit twig:
services:
xx_brand.admin.brand_brand:
calls:
- [ setTemplate, [edit, xxBrandBundle:Admin:base_edit.html.twig]]
On this case, base_edit is an exact copy of the sonata base_edit, but it loads my custom base_edit_form:
{% use 'xxBrandBundle:Admin:base_edit_form.html.twig' with form as parentForm %}
From here I can exclude the default rendering of the retailers, but can't find a way to then render it as I want, as I am not sure how the retailers entity is managed here:
{% if admin.formfielddescriptions[field_name] is defined and field_name != 'retailers' %}
{{ form_row(form[field_name])}}
{% else %}
<ul>
<li>retailer1</li>
<li>retailer2</li>
</ul>
{% endif %}
2 - For the approach of a custom field type, I tried to follow the documentation
Creating the Bundle/Form/Type/ListType.php
Creating the /BrandBundle/Resources/views/form/list.html.twig
Using the ListType in configureFormFields:
use XX\BrandBundle\Form\Type\ListType;
...
->add('retailers', 'ListType');
But I then get an error XX\BrandBundle\Form\Type\ListType
So basically, because I couldn't get it to work, are any of these two options good to solve my issue ?
If so, could anyone please advice on what I am missing there ??
Any help will be very much appreciated :)
You can use sonata_type_model_list: https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#example
Like so:
class BrandAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('retailer', 'sonata_type_model_list', array(
'btn_add' => false,
'btn_delete' => false,
));
}
}
I want to display different messages to a page built by Symfony depending on the situation. I initially set it up by creating a hidden field and setting the label to whatever message I want:
$builder->add('pageTopMsg', 'hidden', array(
'label' => $this->getPageTopMsg(),
'required' => false,
))
That works, but it doesn't feel right. Plus Symfony says I have to create getters and setters in an entity. The messages and the hidden field don't have any relationship with an entity. Is there a better way to display messages on a form dynamically.
To display a form field's value with twig:
{{ form.vars.value.pageTopMsg }}
"form" is the name of your form, and pageTopMsg is your field
But if you just want to show a text not related to you entity, you can pass it from the Controller and show it with {{ pageTopMsg }}
If the field does not have any relation with an entity, you can add this field directly in your Twig form and pass values through out your controller
$this -> render('entity/edit.hmtl.twig', array('someParameters' => array('val1', 'val2')));
I've just started a new Symfony2 project where I've used the generate:doctrine:crud to quickly scale out a few views, forms, etc.
The generated form code is just:
{{ form(form) }} but includes a generic create or delete button. I was wondering how I could add a class to these generic buttons or modify them in any way since it's just encompassed in {{ form(form) }}?
For reference I'm using Twitter Bootstrap to quickly apply some styles so I don't want to change the css based on the submit button.
Thanks!
You can specify CSS classes in the form builder class to avoid filling your Twig template with html even for rendering the form individually.
When you call {{ form(form) }} you are using a helper to simplify your code so you don't have to call form_widget for each one of your fields, but doing so you can't control the exact display in the template. To do it you have to specify the class that will be applied to the field.
In the WhateverType.php file, inside the Forms folder, you have the form builder. There you should have something like:
$builder
->add('text')
->add('whatever')
There you have to add the classes:
$builder
->add('text', 'attr'=> array('class'=>'btn')
->add('whatever')
Then, when your form is displayed in the template, it will apply the classes that you specified in the builder.
After following dmnptr's answer (breaking form into parts), you can pass an array of arguments to each form / form_row / form_label etc by:
{{ form(form, { 'attr': { 'class': 'your-css-class-1 your-css-class-2' } } ) }}
The attr param sets attributes for the item, so the above would produce:
<form class="your-css-class-1 your-css-class-2" ...
You are going to have to render each filed by hand and add necessary classes to elements in your TWIG. Read on how to do it in the official docs - http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand
I need your help. I have build a form of user registration. I need to add checkbox with this massage: "Accept the terms of use" but the word of "terms" must be link to pdf file. Of course I can't put html tags into translation file because it doesn't work. I could use 'raw' keyword in twig template. But I don't want to doing this. I am thinking about placeholder to do it. But I do not now how to made it form class. Do you have any idea how to do it?
class SupplierAddressFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('roules_acceptance', 'checkbox', array('label' => 'form.roules_acceptance', 'mapped' => false));
// ....
}
}
example of label what I need to made:
Accept the terms
By placeholder, are you talking about html one? You can put any html tag in any form type by using the attr option.
For example:
$builder->add('field', 'text', array('attr' => array('foo' => 'bar')));
will generate:
<input type="text" foo="bar" />
Hope, this is what you're looking for!
EDIT:
You issue is about templating, so I recommend you to play with the form theming. For example, you have a form named user_form and a field named field, then, create a twig template with a block named user_form_field_label and put your html as you want in this block. Then, just need to apply your theme on your specific form by using the form_theme twig primitive.
Everything is explained here.
My approach would be not to put the html in the label but to use a 'normal' label, e.g. 'terms'.
The link I would set in the twig template, e.g.
Accept the {{ form_label(your-form-item) }}
Hope that helps.
Thx for yours help.
Finally I sloved it by using twig template. Finally I solved it by using twig template. My example below explain everything:
<div>
{{ form_widget(form.sfGuardUserProfile.roulesAcceptance) }}
{{ 'form.roules_acceptance'| trans | raw }} {# <---- my answer #}
{{ form_errors(form.sfGuardUserProfile.roulesAcceptance) }}
</div>
<div>
{{ form_widget(form.sfGuardUserProfile.allowToUsePersonalData) }}
{{ form_label(form.sfGuardUserProfile.allowToUsePersonalData) }}
{{ form_errors(form.sfGuardUserProfile.allowToUsePersonalData) }}
</div>