10
votes

FormType:

class BranchFormType extends AbstractType {

 public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('name');
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\MainBundle\Entity\Branch',
        ));
    }

}

In entity definition:

<field name="name" column="name" type="string" length="255"/>

there is no nullable=true and the field has required attribute when rendered.

Validation.yml:

My\MainBundle\Entity\Branch:
    properties:
        name:
            - NotBlank: ~

Does symfony find this file automatically or do I have to included somewhere? The doc just states that the form uses validation service automatically.

Controller:

    $branch = new Branch();
    $form = $this->createForm(new BranchFormType(), $branch);

    if ($request->isMethod('POST')) {

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em->persist($branch);
            $em->flush();

            return $this->redirect($this->generateUrl('view_branch'));
        }
    }

    return $this->render('MyMainBundle:Branch:create.html.twig', array(
                'form' => $form->createView()
            ));

On submit I get following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

So isValid returns true despite the fact that name is empty and continues to persist the entity with empty values. How can isValid return true when the name is empty? ORM definition says nullable=false?`Any ideas? I have no extra validator defined. I use sf2.1

5
nullable=false is a Doctrine constraint, isValid does not perform checks on the Doctrine side (Doctrine does not know how to validate en entity), it check only for assertions (NotBlank...) from the Validator component.Damien
I guess there could be a problem with a cascade persist. Let say that your Branch entity has relation to another entity (with cascade persist) which have a "name" field as well. In your case if you set value for the "name" field in your Branch entity but don't set "name" field in related entity you'll got the same error as you described. So could you ensure that your relations don't cause this error? I maybe could say more if you post you Branch entity definitionCyprian
You may be right, In sf toolbar there is an entity error, like "children refers to the owning side field Kloppe\SmsBundle\Entity\User#user which does not exist." here is the entity definition dpaste.com/879513Upvote
i think u need required=true in the formMirage
Does your Branch definition have the getBranch() and setBranch() methods defined?cheesemacfly

5 Answers

2
votes

It looks like the validation file is in the right folder:

src/My/MainBundle/Resources/config/validation.yml

Make sure you are not using validation groups?

Anyway, I would use Assert to see if there is something wrong with this validation file:

Enable annotations:

# app/config/config.yml
framework:
    validation: { enable_annotations: true }

Set up your entity:

namespace My\MainBundle\Entity\;

use Symfony\Component\Validator\Constraints as Assert;
//...

class Branch{

    /**
     * @Assert\NotBlank
     */
    protected $name;

}

Can you try this?

Also, you can test the validation like this from your controller:

// ...
use My\MainBundle\Entity\Branch;

public function indexAction()
{
    $branch = new Branch();

    $branch->setName('');

    $validator = $this->get('validator');
    $errors = $validator->validate($branch);

    if (count($errors) > 0) {
        return new Response(print_r($errors, true));
    } else {
        return new Response('Entity is valid');
    }
}
2
votes
1
votes

You should check if the validation service has correctly aggragated your mapping:

$validator = $this->container->get('validator'); // do it your way

$metadata = $validator->getMetadataFactory()->getClassMetaData('My\\MainBundle\\Entity\\Branch');

var_dump($metadata);

This will permit you to check if your yml configuration has been taken into account.

If not, check at the conventions used to name and place your validation yml file, even if it seems ok.

0
votes

Try adding a NotNull constraint in addition to the NotBlank one.

0
votes

Have you tried clearing the dev cache since creating validation.yml? I was having a similar cause of frustration, and this fixed everything for me.