I have a registration form - from FOSUserBundle this is in the template:
{% form_theme form 'AcmeMyBundle:Form:errors.html.twig' %}
<form class="big-spacer" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
<div>
<input class="btn little-spacer" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
</form>
Here is errors.html.twig
:
{% block field_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<ul class="little-spacer nav text-error">
{% for error in errors %}
<li>{{loop.index}}. {{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock field_errors %}
I have just added some css classes from TwitterBootstrap.
The problem is that I get some of the messages for the validation twice.
My form has 4 fields - Username, Email, Password, Confirm Password
I tried to break as many validation rules I can and here is the output:
For Username
:
For email
:
and if I enter already used email, the error is shown only once:
And for the passwords
:
If they are short:
And if they don't match:
Another strange thing is that when I resubmit the form, but it's still not valid, the notice for the length of the password is only one:
and before resubmitting, they were two.
Do you have any ideas why some of the errors are displayed twice and how to fix this? Thank you very much in advance! :)
UPDATE
This is C:\xampp\htdocs\Project\src\Acme\MyBundle\Entity\User.php
namespace Acme\MyBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
And in C:\xampp\htdocs\Project\app\Resources\FOSUserBundle\translations\
I copied the file validators.en.yml
and in it and removed the [-Inf, Inf]
part and changed the messages a bit.
I also overrode the validation file - I copied it here:
C:\xampp\htdocs\Project\src\Acme\MyBundle\Resources\config\validation.xml
I changed only the minimum length of the password. Everything else is the same as in the original file.
My bundle extends FOSUserBundle:
C:\xampp\htdocs\Project\src\Acme\MyBundle\AcmeMyBundle.php
this file contains the following:
<?php
namespace Acme\BudgetTrackerBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeBudgetTrackerBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Basically, validation makes sure that the provided text is in the right format (e.g., for email, [email protected]), and if the text fits the qualifications for a suitable entry (e.g., the email isn't already registered, or the password fits the criteria).
In short, inline validation is an effort on behalf of the form to help users correct information as they go, ideally so that when the point comes to submit the form, they are more likely to submit it on the first attempt. The more failed attempts a user makes to submit the form, the more likely they are to give up.
Definition. The form error deviation of the real (manufactured) feature (i.e., line, axis, surface, center plane) from the geometrically ideal nominal feature measured and evaluated by analogy to the definition of the form tolerance in ISO 1101.
Ok, this is a known issue.
Let's have a look here : https://github.com/symfony/symfony/issues/2605
The solution is : create your own validation group for properties' validation rules you want to override. In your validation.xml, put only properties you want some different rules, and define validation on a new validation group.
So, for validation.xml, to modify only plainPassword validation rules for your entity (Acme\MyBundle\Entity\User) :
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="Acme\MyBundle\Entity\User">
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
<option name="groups">Registration</option>
</constraint>
<constraint name="Length">
<option name="min">6</option>
<option name="minMessage">fos_user.password.short</option>
<option name="groups">
<value>RegistrationAcme</value>
<value>ProfileAcme</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
And now you have to state that you use different validation groups for the impacted forms (registration and profile). Fortunately, FOSUserBundle is a good practices' example and allows you to override them in your config.yml :
fos_user:
registration:
form:
validation_groups: [Default, RegistrationAcme]
profile:
form:
validation_groups: [Default, ProfileAcme]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With