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 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?