Since I am extending the BaseUser
, I don't have the property $name
in my user entity. How can I add validation to it? Can I just add a property $username
to this entity? Would that mess with or override certain functionality or validation or such that is provided natively to a user class?
I rad this question: FOSUserBundle - Validation for username, password or email fields.
But since I am using a bunch of annotation validations, that won't work for me.
Follow-up question - I see that in fosUSerBundle
XML file, for validating the user that it has natively a checker for use of a taken username. I don't get that functionality, mine goes straight to a SQL error message if I try to add a user with the same name as an existing user. Is this somehow because I am using annotations which overrides the XML validation file all together?
<?php
namespace BizTV\UserBundle\Entity;
use BizTV\UserBundle\Validator\Constraints as BizTVAssert;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use BizTV\BackendBundle\Entity\company as company;
class User extends BaseUser implements AdvancedUserInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//TODO: Add regex on $name for a-z, A-Z, 0-9 and _ (underscore)
//TODO: Add constraint on $name * @BizTVAssert\NameExists (and finish coding this constraint)
/**
* @var object BizTV\BackendBundle\Entity\company
*
* @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company")
* @ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
*/
protected $company;
/**
* @var object BizTV\UserBundle\Entity\UserGroup
* @ORM\ManyToOne(targetEntity="BizTV\UserBundle\Entity\UserGroup")
* @ORM\JoinColumn(name="userGroup", referencedColumnName="id", nullable=true)
*/
protected $userGroup;
So I have looked here (symfony2 using validation groups in form) and tried that solution by adding this to the controller when building the form. Nothing happens (still jumps straight to the INSERT INTO SQL error)
$entity = new User();
$request = $this->getRequest();
$form = $this->createForm(new newUserType($tempCompany), $entity, array('validation_groups'=>'registration'));
$form->bind($request);
I have also tried to set this validation group "registration group" (that is supposed to give you unique user & email validation "for free") this way, in the form class:
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => array('registration'),
));
}
Still nothing happens. Straight to the SQL error upon save.