Display error each field with twig - php

I have a problem with displaying errors with twig.
The errors are displayed in the last form_error with twig and I do not know how to fix it to display each error in his field.
I use annotation for constraints in my entity like * #Assert\True(message = "This field is empty")
{% trans_default_domain 'FOSUserBundle' %}
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register" novalidate="novalidate">
{{ form_errors(form) }}
<article class="form-part part1">
<h2>1 - Information pour la création du compte</h2>
<label for="{{ form.pays.vars.id }}">Pays <span>*</span> : </label>
{{ form_widget(form.pays) }}
{{ form_errors(form.pays) }}
<label for="{{ form.username.vars.id }}">Pseudo <span>*</span> : </label>
{{ form_widget(form.username) }}
{{ form_errors(form.username) }}
<label for="{{ form.email.vars.id }}">Email <span>*</span> : </label>
{{ form_widget(form.email) }}
{{ form_errors(form.email) }}
{{ form_row(form.plainPassword) }}
<div class="birthday">
{{ form_row(form.birthday) }}
</div>
{{ form_row(form.nationalite) }}
</article>
<article class="form-part">
<h2>2 - Télécharger les 6 images de votre galerie</h2>
<div>
{{ form_row(form.images, {'label': ' '}) }}
</div>
</article>
<div class="bottom-form">
<div class="contrat-cgu">
<div>
{{ form_label(form.certificatAge) }}
{{ form_widget(form.certificatAge) }}
{{ form_errors(form.certificatAge) }}
Lire les Conditions générales d'utilisation
</div>
<div>
{{ form_label(form.artistContract) }}
{{ form_widget(form.artistContract) }}
{{ form_errors(form.artistContract) }}
Lire le Contrat artiste
</div>
</div>
{{ form_row(form.how) }}
{{ form_rest(form) }}
</div>
If someone has an idea

Related

Form errors not appear in a custom form theme with EasyAdmin3

I overrided my form in EasyAdmin to organize differently my input so I did like this :
In my Crud :
public function configureCrud(Crud $crud): Crud
{
return $crud
->setPageTitle(Crud::PAGE_INDEX, 'admin.menu.infos')
->setFormThemes(['bundles/EasyAdminBundle/crud/company/edit.html.twig'])
;
}
And in my Twig :
{% extends '#EasyAdmin/crud/form_theme.html.twig' %}
{% use '#EasyAdmin/symfony-form-themes/bootstrap_5_layout.html.twig' %}
{% block form %}
{{ form_start(form) }}
<div class="row">
{{ form_row(form.logo) }}
{{ form_row(form.phone) }}
{{ form_row(form.catchphrase) }}
</div>
<div class="row">
{{ form_row(form.businessName) }}
{{ form_row(form.email) }}
{{ form_row(form.website) }}
</div>
<div class="row">
{{ form_row(form.companyName) }}
{{ form_row(form.address) }}
{{ form_row(form.linkedin) }}
</div>
<div class="row">
{{ form_row(form.siren) }}
{{ form_row(form.zipcode) }}
{{ form_row(form.facebook) }}
</div>
<div class="row">
{{ form_row(form.legalForm) }}
{{ form_row(form.city) }}
{{ form_row(form.twitter) }}
</div>
<div class="row">
{{ form_row(form.description) }}
</div>
{{ form_end(form) }}
{% endblock %}
The form is like I wanted and when I submit, it's work but the problem is when I make an error. For example if I valid with an empty value, then instead of getting my form_errors, I've got a 500.
I've tried different things but none of them worked :
block errors :
{{ form_start(form) }}
{% block form_errors %}
{{ parent() }}
{% endblock %}
individual error :
{{ form_errors(form.businessName) }}
Any idea ?

Symfony - How to format an entity field in a form (to display more fields than just title)?

I have a form where user can submit a new Mandate. Every Mandate can have multiple Profiles linked to it.
Here is how the form looks like at this moment:
The Profiles field is added to the form like that (from MandateController):
$form = $this->createFormBuilder($mandate)
->add('content')
->add('name')
->add('company_name')
->add('budget')
->add('phone')
->add('email')
->add('description')
->add('profiles', EntityType::class, array(
'multiple' => true,
'expanded' => true,
'class' => 'KrownDashboardBundle:Profile',
'choice_label' => 'title',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.title', 'ASC');
},
))
->getForm();
And the view is handled this way:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
<div class="col_full">
<label for="" class="control-label">{{ form_label(form.content) }}</label>
{{ form_widget(form.content, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.content) }}
</div>
<div class="col_full">
<label for="" class="control-label">Prénom et nom</label>
{{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.name) }}
</div>
<div class="col_full">
<label for="" class="control-label">Nom de lentreprise</label>
{{ form_widget(form.company_name, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.company_name) }}
</div>
<div class="col_full">
<label for="" class="control-label">Budget</label>
{{ form_widget(form.budget, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.budget) }}
</div>
<div class="col_full">
<label for="" class="control-label">Numéro de téléphone</label>
{{ form_widget(form.phone, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.phone) }}
</div>
<div class="col_full">
<label for="" class="control-label">E-mail</label>
{{ form_widget(form.email, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.email) }}
</div>
<div class="col_full">
<label for="" class="control-label">Description</label>
{{ form_widget(form.description, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.description) }}
</div>
<div class="col_full">
<label for="" class="control-label">Profiles</label>
{{ form_widget(form.profiles) }}
{{ form_errors(form.profiles) }}
</div>
</div>
<button id="login-form-submit" class="button button-3d button-black nomargin" value="Log in" name="login-form-submit">Ok</button>
{{ form_end(form) }}
I want to modify the Profiles listing, so they look like this (all these fields are saved into the Profile entity):
Instead:
How can I achieve that? What's the best way to do it?

Issue with resizing fields of form under symfony 3

I have a problem with resizing the fields I've generated with symfony, my fields take the whole width of the page. Here is my code:
Controller :
$user= new User();
$form=$this->CreateFormBuilder($user)
->add("firstName",TextType::class)
->add("lastName",TextType::class)
->add("login",TextType::class)
->add("password",PasswordType::class)
->add("tel",TextType::class)
->add("email",TextType::class)
->add("address",TextType::class)
->add("send",SubmitType::class)
-> getForm();
$form->handleRequest($req);
if($form->isValid())
{
$em=$this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
return $this->render('eplateformeBundle:Default:register.html.twig',['form'=>$form->createView()]);
The view :
<div class="well">
{{ form_start(form ) }}
{{ form_errors(form) }}
{{ form_row(form.firstName) }}
{{ form_row(form.lastName) }}
{{ form_row(form.login) }}
{{ form_row(form.password) }}
{{ form_row(form.tel) }}
{{ form_row(form.email) }}
{{ form_row(form.address) }}
{{ form_widget(form.send, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
You can transfer own class to inputs fields like this:
<div class="well">
{{ form_start(form ) }}
{{ form_errors(form) }}
{{ form_row(form.firstName,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.lastName,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.login,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.password,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.tel,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.email,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.address,{'attr':{'class':'your own class''}}) }}
{{ form_widget(form.send, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>

How to customize FOSUserBundle forms

I have below problem with FOSUserBundle customization.
I was overwritten FOSUser register form, then I changed register_content from
{{ form_widget(form) }}
to:
<div class="col-md-6">
{{ form_label(form.firstname) }}
{{ form_errors(form.firstname) }}
{{ form_widget(form.firstname) }}
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
<div class="col-md-6">
{{ form_label(form.email) }}
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
{{ form_label(form.plainPassword) }}
{{ form_errors(form.plainPassword) }}
{{ form_widget(form.plainPassword) }}
{{ form_label(form.plainPassword.second) }}
{{ form_errors(form.plainPassword.second) }}
{{ form_widget(form.plainPassword.second) }}
</div>
But, when I was went to the browser, I got it:
bad form render
The form renders incorrectly. How I can repair this?
try with this
{{ form_label(form.plainPassword.first) }}
{{ form_errors(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_label(form.plainPassword.second) }}
{{ form_errors(form.plainPassword.second) }}
{{ form_widget(form.plainPassword.second) }}

Prevent form fields from rendering

I'm trying to control the rendering of password fields based on whether i'm editing an user or creating one. I'm doing this with a simple session boolean variable as follows:
{{ form_start(userForm) }}
{{ form_errors(userForm) }}
<div id="user-fg-email" class="form-group">
{{ form_label(userForm.email) }}
{{ form_errors(userForm.email) }}
{{ form_widget(userForm.email) }}
</div>
{% if app.session.get('editingUser') == false %}
<div id="user-fg-pp1" class="form-group">
{{ form_label(userForm.plainPassword.first) }}
{{ form_widget(userForm.plainPassword.first) }}
</div>
<div id="user-fg-pp2" class="form-group">
{{ form_label(userForm.plainPassword.second) }}
{{ form_errors(userForm.plainPassword.first) }}
{{ form_widget(userForm.plainPassword.second) }}
</div>
{% endif %}
<div id="user-fg-role" class="form-group">
{{ form_label(userForm.role) }}
{{ form_errors(userForm.role) }}
{{ form_widget(userForm.role) }}
</div>
<button type="submit" class="btn btn-primary pull-right">Submit</button>
{{ form_end(userForm) }}
However when this boolean is evaluated as true, which is supposed to prevent these fields from rendering, they are still being rendered assumingly by the later following form_end tag.
Is there a way to prevent that from happening?
edit:
if editingUser == true the password fields are actually rendered after the button, hence my assumption it's done so by the form_end tag.
Because you have to specify Twig to not display all the rest of the fields which are not explicitly rendered in the form : http://symfony.com/doc/current/reference/forms/twig_reference.html#form-end-view-variables
{{ form_end(form, {'render_rest': false}) }}

Categories