Francisco Albert
Francisco Albert

Reputation: 1711

No default option is configured for constraint

I was writing my own constraint (silly simple) following the documentation for symfony 2. But after run it I have this error:

No default option is configured for constraint Cgboard\SignupBundle\Validator\Constraints\PasswordNotMatching

I saw in another post in stackoverflow a solution by using a service, but I'm not sure about if that solution is the best, in the documentation doesn't appear any service regarding this simple example.

Debugging a little I saw finally that the buildform() method is never called. Why?

My validation.yml

Cgboard\SignupBundle\Entity\SignupData:
  properties:
    email:
      - NotBlank: ~
    nickname:
      - NotBlank: ~
    password:
      - NotBlank: ~
    password_repeat:
      - NotBlank: ~
      - Cgboard\SignupBundle\Validator\Constraints\PasswordNotMatching: ~;

My validator:

<?php
namespace Cgboard\SignupBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class PasswordNotMatchingValidator extends ConstraintValidator
{
    public function validate($DataForm, Constraint $constraint)
    {
        return false;
    }
} 

My constraint:

<?php
namespace Cgboard\SignupBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class PasswordNotMatching  extends Constraint
{
    public $message = 'The password doesnt match';

}

My controller is this one:

<?php
namespace Cgboard\SignupBundle\Controller\Frontend;

use     Symfony\Component\HttpFoundation\Request;
use     Symfony\Bundle\FrameworkBundle\Controller\Controller;
use     Cgboard\SignupBundle\Entity\User;


class SignupController extends Controller
{
    public $SignupData;

    public function indexAction(Request $request)
    {
        $SignupData = $this->getSignupData();
        $SignupForm = $this->getSignupForm();
        $form       = $this->createForm($SignupForm, $SignupData);

        if($request->getMethod() === 'POST') {
            $form->bind($request);
            if($form->isValid()) {
                $this->save($SignupData);
            }
        }

        return $this->render(
                                'CgboardSignupBundle:Frontend:signup.html.twig',
                                array('form'=>$form->createView())
        );
    }

    public function save(\Cgboard\SignupBundle\Entity\SignupData $SignupData)
    {
        // something

    }

    public function getSignupData()
    {
        return new \Cgboard\SignupBundle\Entity\SignupData();
    }

    public function getSignupForm()
    {
        return new \Cgboard\SignupBundle\Forms\Frontend\SignupForm();


    }

} 

My form is:

<?php
namespace Cgboard\SignupBundle\Forms\Frontend;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;


class SignupForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        die('im here');                        // never is called, why??
        $builder->add('email');
        $builder->add('nickname');
        $builder->add('password');
        $builder->add('password_repeat');
    }

    public function getName()
    {
        return 'signup_form';
    }
} 

I was debugging and I saw that the method buildform() is not called ever. Why??

Upvotes: 2

Views: 3731

Answers (2)

Tom
Tom

Reputation: 2322

For anyone who will see this thread, and uses XML instead of YAML, then this could also be a possible solution for the same error message:

This will not work:

<getter property="fieldname">
    <constraint name="Test\TestBundle\Validator\Constraints\Test">
    </constraint>
</getter>

but this will work:

<getter property="fieldname">
    <constraint name="Test\TestBundle\Validator\Constraints\Test" />
</getter>

Upvotes: 1

Wouter J
Wouter J

Reputation: 41934

You should use ~ instead of ~; in your validation.yml:

Cgboard\SignupBundle\Entity\SignupData:
  properties:
    email:
      - NotBlank: ~
    nickname:
      - NotBlank: ~
    password:
      - NotBlank: ~
    password_repeat:
      - NotBlank: ~
      - Cgboard\SignupBundle\Validator\Constraints\PasswordNotMatching: ~

Explaination

Cgboard\SignupBundle\Validator\Constraints\PasswordNotMatching: ~; means something different than ...: ~. The ~ operator means null in Yaml. When doing : ~, you configure it with null (nothing). But when doing ~; you configure it with the string "~;".

There are 2 ways to configure options for a constraint: using an array:

Choice:
  choices: [red, blue]

But some constraints have a "default option". For the Choice constraint, choices is the default options. That means that the above can be done as follows:

Choice: [red, blue]

Now, [red, blue] will be set to the choices option.

In your case, you defined the value "~;". Since this was not null, it tried to set that to the default option. But the constraint didn't had a default option configured, so Symfony failed. When using ~, it is null and Symfony won't set any option.

Upvotes: 0

Related Questions