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>
Related
I have created a custom form type - RecaptchaType. I added it into many forms using $builder->add('recaptcha', RecaptchaType::class). For view I have created a macro, so in twig I render it as follows {{ forms.recaptcha(form.recaptcha) }}.
But now I want to display recaptcha only for users who are not logged in. In twig macro I can simple add if condition. But how can I achieve not adding (or removing) recaptcha input in form type if I want to edit only RecaptchaType and don't want to touch form types, which are using this.
I had two ideas, but neither of them did work.
In RecaptchaType use function buildForm and do $builder->remove('recaptcha') when condition is met (user is logged in)
Pass option 'render' to $resolver->setDefaults(), access it in form and depend on value do ->add or not. But I can't access the value.
How would you do that? I can add code example if needed.
TL;DR; To remove a form type conditionally encapsulating this logic within itself, you can do the follows:
RecaptchaType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
if ($this->isAuthenticated()) {
$form = $event->getForm();
$form->getParent()->remove($form->getName());
}
});
}
Now, if you are customizing the form theme make sure to check the existence of the field before render it:
{% if form.recaptcha is defined %}
{{ forms.recaptcha(form.recaptcha) }}
{% endif %}
It should work as long as your RecaptchaType is embedded into another form.
I use almost standard CRUD form rendering in Twig:
{{ form_start(form, {'attr' : {'class' : 'new_edit_form'}}) }}
{{ form_widget(form) }}
<input type="submit" value="{{ 'default.new.create'|trans }}"/>
{{ form_end(form) }}
In this case all inputs have labels with upper case property names:
Title
Description
Date
I have translations enabled in my project and Twig tries to translate these words, but in my dictionaries all properties have ids like field.property_name.
So I want to go through all form inputs and rewrite all labels making them lower case and adding 'field.' prefix. How can I do this in cycle?
Symfony 3.4
In order to overwrite the translations you need to add the messages.en.yml to the app/YourBundle/Resources/translations and overwrite the messages you want like:
field.property_name: Your label here
This can help: How to translate labels in symfony2 forms with messages.en.yml?
You can define a specific label for each field:
$builder->add('save', TextType::class, array(
'label' => 'field.sth'
))
I try to render a few fields in my form, not all, but twig always render all fields. It is my twig code:
{{ form_start(form) }}
{{form_widget(form.subject)}}
{{form_widget(form.name)}}
{{form_widget(form.parent,{'value' : ''})}}
{{form_widget(form.save)}}
{{ form_end(form) }}
Set type for field you don't want visible to hidden. your form type would look something like this:
class ConfigurationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', HiddenType::class)
// more fields
}
// other configurations
}
The form_end function renders the elements that have not been rendered yet. Read here: http://symfony.com/doc/current/reference/forms/twig_reference.html#form-end-view-variables.
This helper also outputs form_rest() unless you set render_rest to
false
You can disable that behaviour as you can see in the docs.
Anyway you should render all fields for validation to work, for example, and for security reasons (as it will add validation for CSRF automatically).
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 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