Validation of a form - I'm getting the labels two times

前端 未结 2 1470
清歌不尽
清歌不尽 2021-01-02 18:51

I have a registration form - from FOSUserBundle this is in the template:

{% form_theme form \'AcmeMyBundle:Form:errors.html.twig\' %}

   
相关标签:
2条回答
  • 2021-01-02 19:17
    # config.yml
    # ...
    fos_user:
        db_driver:     orm
        firewall_name: main
        user_class:    Acme\YourBundle\Entity\User
        registration:
            form:
                type: acme_user_registration
                validation_groups: [Registration]
    # ... 
    

    Adding the last line ("validation_groups: [Registration]") results in only one error. Only FOSUserBundle validation: "Registration".

    0 讨论(0)
  • 2021-01-02 19:25

    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]
    
    0 讨论(0)
提交回复
热议问题