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
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 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>
I want to retrieve Cities names from a table in the database and put them as options in a select input (combobox) which is defined in 'layout.html.twig' . All my views extends 'layout.html.twig', so how can I access to cities names in every page?
[Solution]
I'm not able to respond to my topic ,I didn't have much reputation so I edit my topic
I have found the solution, using "embedding controllers"
first I've created an action to retreive all cities names:
public function listCitiesAction(){
// retreiving cities
$entities = $this->getDoctrine()->getRepository("MedAdBundle:City")->findAll();
return $this->render('MedAdBundle:Ville:list_cities.html.twig',
array('entities' => $entities));
}
this action render list_cities.html.twig defined as :
<select class="form-control">
{% for entity in entities %}
<option>{{ entity.name}}</option>
{% endfor %}
</select>
finnaly I edit my layout.html.twig
<div>
{{ render(controller('MedAdBundle:City:listCities'))}}
</div>
In this way I can access to cities combobox in every page in my app ;)
Another nice way would be use render.
This allows you to call a controller out of your layout.html.twig
{{ render(controller("AcmeDemoBundle:Helper:citySelector")) }}
you also can cache the output with ESI.
It's well explained in the cookbook.
http://symfony.com/doc/current/cookbook/templating/global_variables.html
I would go with these steps:
Write a form hosting a symfony entity field configured to work with the Cities table
Define this form as a service in the DIC
Define a twig extension that exposes a function to output the form HTML
Use the twig function in the layout.html.twig that is extended by all the other templates
As an optimization I would look how I could wire Doctrine with some caching system (e.g. memcached) to avoid hitting the database on each page load.
This is where you can find documentation about the entity field: http://symfony.com/doc/current/reference/forms/types/entity.html
Use the Symfony documentation to find how to define a form as a service and how to write your own twig extension.
When I want to split my form in twig, I use form_widget of form_row, but I have to write the form tag manually
<form action='path("form_action_path")' method='...'>
Is there a function in twig that allow me to generate this tag automatically?
As of Symfony 2.3, there is a form_start function:
{{ form_start(form) }}
I used to work with CodeIgniter. Now I am starting to learn Symfony2. I was just wondering, in CodeIgniter I could load a view from another view. Like, I could load menu.php from index.php. This is how I used to do it -
//in index.php
<?php $this->load->view('menu.php'); ?>
Is it possible to do the same thing in Symfony2 and Twig?
There are a few different ways you can do it depending on what you're trying to accomplish.
If you want to render the response of a controller you can do this in your twig template.
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
In the above example, the parameter passed max would be passed as an argument to your controller. Then the controller would be responsible for returning a response that will be inserted into the view where it was called from.
You can also use include to render just the twig template as an embedded view:
{% for article in articles %}
{{ include(
'AcmeArticleBundle:Article:articleDetails.html.twig',
{ 'article': article }
) }}
{% endfor %}
In the above example article would be passed into the context of the twig template articleDetails.html.twig but not to any controller. So this method is ideal for repetitious front-end code that is used in many places such as templates for tables, lists, sidebars, etc.
http://symfony.com/doc/current/book/templating.html#including-other-templates
http://twig.sensiolabs.org/doc/functions/include.html