dousha1873 2016-10-26 16:31
浏览 100
已采纳

Symfony表单验证实体

I have two entoty User and Location and I crate model with two entity and create form for this model and add validate_group for this form? but ahen I check form is valid - form always valid, but entity is emthy and entity have assert not blank fields, what I'am doing wrong ?

entities

class User implements UserInterface, \JsonSerializable
{
use GedmoTrait;

/**
 * @var integer
 *
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @Assert\NotBlank(groups={"admin_user_post"})
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $firstName;


class Location
{
/**
 * @var integer
 *
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @Assert\NotBlank(groups={"admin_user_post"})
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $address;

create form

class CreateUser extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('user', new UserType(), ['validation_groups' => ['admin_user_post']]);
    $builder->add('location', new LocationType(), ['validation_groups' => ['admin_user_post']]);
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AdminBundle\Model\CreateUserModel',
        'csrf_protection' => false,
        'validation_groups' => ['admin_user_post']            
    ));
}

    /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstName')
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\User',
        'csrf_protection' => false,
        'validation_groups' => ['admin_user_post']
    ));
}

    /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('address')
        ->add('cityObject', null, array('attr' => array('placeholder' => 'Select city')));
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Location',
        'csrf_protection' => false,
        'validation_groups' => ['admin_user_post']
    ));
}

and action

        $entity = new CreateUserModel();

    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()
        && $form->get('user')->isValid()
        && $form->get('location')->isValid()
    ) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity->getLocation());
        $entity->getUser()->setLocation($entity->getLocation());
        $em->persist($entity->getUser());
        $em->flush();
        $user = $entity->getUser();

        return $this->redirect($this->generateUrl('admin_users_show', array('id' => $user->getId())));
    }

    /**
 * Creates a form to create a User entity.
 *
 * @param CreateUserModel $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(CreateUserModel $entity)
{
    $form = $this->createForm(new CreateUser(), $entity, array(
        'validation_groups' => ['admin_user_post'],
        'action' => $this->generateUrl('admin_users_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

I try in action

$error = $this->get('validator')->validate($form->getData()->getUser(), ['admin_create_user']);

but still have empty $error

Why form is valid true ? or how correct valid form model with my entities and assert in this entities ?

  • 写回答

1条回答 默认 最新

  • dqmgjp5930 2016-10-26 22:41
    关注

    Add to 'CreateUser' form 'cascade_validation' option to validate nested forms, and check that's annotation method for your constrains was specified at config.yml

    # app/config/config.yml
    framework:
        validation: { enable_annotations: true }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名