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).
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 have a simple form and i want display another form after submitting in the same template twig
mycontroller:
/**
* #Route("/admin/client/modifier", name="modifier")
* #Method({"GET","POST"})
*/
public function modifierAction(Request $request)
{
$form=$this->createFormBuilder()
->add('raisonSocial', TextType::class)
->add('Rechrecher', SubmitType::class)
->getForm();
if($form->isSubmitted() && $form->isValid()){
} ;
return $this->render('myTemplate/Clients/modifierClient.html.twig');
}
You have to adapt your answer to the correct way. I do not remember exactly the correct $request->request->get syntax so check the documentation.
Anyway... I guess you can achieve this by using a render controller inside your twig based on some parameters that come from the post/get of your form submission or via ajax (but you already said you don't know how to do this via ajax).
So let's say you search "test" you should have inside your $request->request->get your parameter test.
you should to something like this in your twig file:
your div where you want to have the form displayed
<div>
{% if app.request.get('your_key') == 'test' %}
{{ render(controller(''AppBundle:Article:recentArticles'')) }}
{% endif %}
</div>
This means that you will need to create yourself a new action in ArticleController called recentArticles where you will have your new form.
You can find more details about this on the symfony documentation here:
https://symfony.com/doc/current/templating/embedding_controllers.html
Is it possible to add extra string value to the form and then render it in template like this:
form class:
public function buildForm(FormBuilderInterface $builder, array $options)
{
// add your custom field
$builder->setSomeString('name', 'value');
template:
{{ form_start(form) }}
{{ form_getSomeString('name')
First, you have to set your values in the buildView() function (here is an example from the core) instead of buildForm().
Then you have to override your form block in twig to access this value (see documentation).
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>
On a form classe's buildForm method (AbstractType derived) can I set the action of that form?
What I want to do is similar to the setAction method that I can use when building an embedded form:
$form = $this->createFormBuilder()
->setAction($this->generateUrl('my_action'))
->add('field', 'text')
->add('button', 'submit');
I mean, is the setAction an equivalent to form classes?
You get access to the same form builder in the buildForm method, so just calling the setAction on it will work:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setAction($path);
}
The question is how you pass the $path to your form type. One of the ways would be to pass it as an option when creating the form. But if you're passing the $path anyway, why not just set the action itself?
$form = $this->createForm(new MyType(), $object, array(
'action' => $this->generateUrl('my_action'),
));
Another way would be to inject the router to the form type and use it to generate the URL, but I don't think it's a good idea to make that kind of decisions in a form type.