Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Created April 13, 2015 10:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/021cf8ac434eb5af9144 to your computer and use it in GitHub Desktop.
Save webdevilopers/021cf8ac434eb5af9144 to your computer and use it in GitHub Desktop.
Remove email validation group on FOSUser with SonataUser
fos_user:
db_driver: orm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
group:
group_class: Application\Sonata\UserBundle\Entity\Group
group_manager: sonata.user.orm.group_manager
profile:
# Authentication Form
form:
type: fos_user_profile
handler: fos_user.profile.form.handler.default
name: fos_user_profile_form
# validation_groups: [Authentication] # Please note : this is not the default value
validation_groups: [Default]
registration:
form:
validation_groups: [Default]
service:
user_manager: sonata.user.orm.user_manager
sonata_user:
impersonating:
route: sonata_admin_dashboard
security_acl: false
manager_type: orm
admin:
user:
class: Application\Sonata\UserBundle\Admin\UserAdmin
class:
user: Application\Sonata\UserBundle\Entity\User
group: Application\Sonata\UserBundle\Entity\Group
profile:
form:
validation_groups: [Profile, Default]
<?php
namespace Application\Sonata\UserBundle\Entity;
//use FOS\UserBundle\Entity\User as BaseUser;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
*
* @ORM\Entity(repositoryClass="Application\Sonata\UserBundle\Entity\UserRepository")
* ORM\Table(name="fos_user")
* @ORM\Table(name="users",
* indexes={
* @ORM\Index(name="index_compartment_id", columns={"compartment_id"}),
* @ORM\Index(name="index_users_on_branch_id", columns={"branch_id"}),
* },
* uniqueConstraints={
* @ORM\UniqueConstraint(name="username_canonical_unique", columns={"username_canonical"})
* }
* )
* @UniqueEntity(fields="usernameCanonical", errorPath="username", message="fos_user.username.already_used")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="email", column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)),
* @ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true))
* })
*/
//class User
class User extends BaseUser
{
}
<?php
namespace Application\Sonata\UserBundle\Admin;
use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends SonataUserAdmin
{
// @see https://groups.google.com/forum/#!topic/sonata-users/lLZyA4VBKOY
protected $formOptions = array(
'validation_groups' => 'default'
);
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('email', null, array('required' => false));
}
}
@webdevilopers
Copy link
Author

This sums up misc solutions provided:

Change validation group on fos_user config:

fos_user:
    profile:
        # Authentication Form
        form:
            type:               fos_user_profile
            handler:            fos_user.profile.form.handler.default
            name:               fos_user_profile_form
#            validation_groups:  [Authentication] # Please note : this is not the default value
            validation_groups:  [Default] 

Change validation group on sonata_user config:

sonata_user:
    profile:
        form:
            validation_groups:  [CustomGroup, Profile, Default]

Changing validation groups on the admin class via formOptions:

    protected $formOptions = array(
        'validation_groups' => 'creation'
    );

Overriding Annotations:

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="email", column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)),
 *      @ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true))
 * })
 */
class User extends BaseUser {
    // ...
}

Improving previous steps:

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="User_User")
 * @ORM\Entity(repositoryClass="UserRepository")
 * @UniqueEntity(fields="usernameCanonical", errorPath="username", message="fos_user.username.already_used")
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="email", column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)),
 *      @ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true))
 * })
 */
class User extends BaseUser
{
...
}

But still Sonata User validates and does not allow a blank email field.

@webdevilopers
Copy link
Author

@psihius
Copy link

psihius commented Aug 3, 2016

I've found why this does not work as it should be.

In UserAdmin class of Sonata Bundle in the getFormBuilder() method they overwrite the validation_groups option without any checks.

$options['validation_groups'] = (!$this->getSubject() || is_null($this->getSubject()->getId())) ? 'Registration' : 'Profile';

@spacetraveller
Copy link

Please fix, this is not really flexible. The point of Sonata should be to be as flexible as possible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment