Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - Symfony2 validation doesn't work when Entity Relationships/Associations

controller

public function indexAction(Request $request)
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    $owner = $user->getId();

    $first = new First();
    $first->setOwner($owner);

    $second = new Second();
    $second->setOwner($owner);
    $second->setFirst($first);

    $form = $this->createForm(new SecondType(), $second);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->get('doctrine')->getEntityManager();
            $em->persist($first);
            $em->persist($second);
            $em->flush();
        }
    }

    return $this->render('MySampleBundle:Home:index.html.twig', array(
        'form' => $form->createView(),
    ));

}

ORM Yaml

MySampleBundleEntityFirst:
    type: entity
    table: first
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
        date_created:
            type: datetime
        date_edited:
            type: datetime
        owner:
            type: integer
    lifecycleCallbacks:
        prePersist: [ prePersist ]
        preUpdate: [ preUpdate ]
    oneToMany:
        reviews:
            targetEntity: Second
            mappedBy: review

MySampleBundleEntitySecond:
    type: entity
    table: second
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        review:
            type: string
        date_created:
            type: datetime
        date_edited:
            type: datetime
        owner:
            type: integer
    lifecycleCallbacks:
        prePersist: [ prePersist ]
        preUpdate: [ preUpdate ]
    manyToOne:
        first:
            targetEntity: First
            inversedBy: reviews
            joinColumn:
                name: first_id
                referencedColumnName: id

Form/Type

class FirstType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', 'text');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'MySampleBundleEntityFirst',
        );
    }

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

class SecondType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('first', new FirstType());
        $builder->add('review', 'textarea');
    }

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

Validation.yml

MySampleBundleEntityFirst:
    properties:
        title:
            - NotBlank: ~
            - MinLength: 2

MySampleBundleEntitySecond:
    properties:
        review:
            - NotBlank: ~
            - MinLength: 14

The created form works normally. However, only the validation does not work normally.

If it performs individually, validation will work normally.

$form = $this->createForm(new FirstType(), $first);

However, if it is in a Entity Relationships/Associations state, the first validation will not work.The First's title property in one character will be registered.

How can I manage to achieve that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Symfony 2.1+ doesn't automatically validate all the embedded objects. You need to put the Valid constraint on the first field to make it validate as well:

MySampleBundleEntitySecond:
    properties:
        first:
            - Valid: ~

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...