2

Env: Symfony2.2 + Propel 1.6

I try to validate a 'customer' form which is linked to the model (Customer) to manage a "create account" (form 1) and a "login account" (form 2). The user is required to check the "I accept the terms of agreement" checkbox and this field is not linked to the model. I'm using a global "validation.yml" file to manage validation rules.

I don't know how to validate the checkbox is checked with the validation.yml file.

I've try several technics:

1/ Put a rule in the validation.yml and add getter/setter in the model:

validation.yml:

MyProject\Model\Customer:
properties:
    email:
        - NotBlank:
            groups:     [login, create]
            message:    Champ obligatoire.
        - Email:
            groups:     [login, create]
            message:    La valeur saisie doit être un email.
    cgv:
        - Symfony\Component\Validator\Constraints\True:
            groups:         [login, create]
            message:        Vous devez accepter les CGV.
constraints:
    - Propel\PropelBundle\Validator\Constraints\UniqueObject:
        groups:         [create]
        fields:         email
        message:        Cet email est déjà inscrit.
    - Callback:
        groups:         [login]
        methods:
            -    [MyProject\Model\CustomerQuery, isCustomerEmail]

MyProject\Model\Customer:

class Customer extends BaseCustomer {
    private $cgv;

    (...)

    public function setCgv($cgv) {
        $this->cgv = (Boolean) $cgv;
    }

    public function getCgv() {
        return $this->cgv;
    }
}

Result: the rule "True" is ok even if the checkbox isn't checked. If I add a "NotBlank" rule, the validation failed in both check/uncheck cases.

2/ Try to add specific validation rules in the "CustomerType" object (like explained in this article)

/*
 * 
 *
 * @author
 */
class CustomerLoginType extends AbstractType {
    /**
     * 
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'text', array('required' => true));
        $builder->add('fill', 'checkbox', array('mapped' => false, 'required' => false, 'data' => true));
        $builder->add('cgv', 'checkbox', array('data' => true, 'mapped' => false, 'required' => true, 'validation_groups' => array('login'), 'constraints' => new True(array('message' => 'Vous devez accepter les Conditions Générales de Vente.'))));
   }

    /**
     * 
     * @return string
     */
    public function getName()
    {
        return 'customer_login';
    }    

    /**
     * 
     * @param \MyProject\FrontBundle\Form\Type\OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyProject\Model\Customer',
            'validation_groups' => array('login')
        ));
    }

}

Result: Theses rules are not take into account if a "validation.yml" file exists, it could work if I remove the "Customer" entry in the file, but I would prefer to keep it if possible.

3/ As I can't find a "yml" solution, I've finally add a "manual" validation inside the controller like this:

    $form_request = $this->getRequest()->get('customer_login');
    if (!isset($form_request['cgv'])) {
        $form_customer_login->get('cgv')->addError(new \Symfony\Component\Form\FormError('Vous devez accepter les Conditions Générales de Vente.'));
    }

Any idea of how I could add my "accept terms" checkbox rule inside the validation.yml file?

Lionel
  • 387
  • 3
  • 18

0 Answers0