Symfony 4 form validation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Symfony 4 form validation



I'm using Symfony 4.1 and I've faced up with form validation.
So I can't understand if it is possible to avoid validation of the all entity fields. For example I have a User entity, that looks like this:




/**
* @ORMEntity
* @UniqueEntity(fields="email", message="Email already taken")
* @UniqueEntity(fields="username", message="Username already taken")
*/
class User implements UserInterface
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORMColumn(type="string", length=255, unique=true)
* @AssertNotBlank()
* @AssertEmail()
*/
private $email;

/**
* @ORMColumn(type="string", length=255, unique=true)
* @AssertNotBlank()
* @AssertLength(
* min = 2,
* max = 50,
* minMessage = "Your username must be at least limit characters long",
* maxMessage = "Your username cannot be longer than limit characters"
* )
*/
private $username;



it is ok for registration but not for login.
My login form type :




class UserLoginType extends AbstractType

public function buildForm(FormBuilderInterface $builder, array $options)

$builder
->add('username', TextType::class)
->add('password', PasswordType::class)
;


public function configureOptions(OptionsResolver $resolver)

$resolver->setDefaults(array(
'data_class' => User::class,
));



Login page has only username and password fields. But symfony shows an email error when I'm trying to insert data.



enter image description here



Profiler:
enter image description here
Please tell me what I'm doing wrong?




1 Answer
1



Yes, you can achieve this using validation groups documented here:
http://symfony.com/doc/current/validation/groups.html


validation groups



Basically use the annotations to specify different groups for registration and login:


* @AssertNotBlank(groups="registration", "login")
* @AssertEmail(groups="registration")
*/
private $email;



Depending on which fields and when you want to validate.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered