Created
April 13, 2015 10:59
-
-
Save webdevilopers/021cf8ac434eb5af9144 to your computer and use it in GitHub Desktop.
Remove email validation group on FOSUser with SonataUser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
} |
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';
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
This sums up misc solutions provided:
Change validation group on
fos_user
config:Change validation group on
sonata_user
config:Changing validation groups on the admin class via
formOptions
:Overriding Annotations:
Improving previous steps:
But still Sonata User validates and does not allow a blank email field.