开发者

Symfony2 change validation group based on field value

I have controller action like this:

public function createAction()
{
    $entity  = new Client();

    $request = $this->getRequest();
    $form    = $this->createForm(new ClientType(), $entity);
    $form->bindRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('client_show', array('id' => $entity->getId())));

    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView()
    );
}

The underlying Client Entity has a field type witch can take values [0, 1] now, I have defined the 2 validation groups for Client entity: person and company.

How can I change/choose validation group开发者_开发技巧 based on user entered value into type field?


You can add a callback validation constraint for your class (Client entity), from the docs:

Classes

Some constraints apply to the entire class being validated. For example, the Callback constraint is a generic constraint that's applied to the class itself. When that class is validated, methods specified by that constraint are simply executed so that each can provide more custom validation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜