簡體   English   中英

Symfony表單驗證實體

[英]Symfony form validate entity

我有兩個實體用戶和位置,並創建了具有兩個實體的模型,並為此模型創建表單,並為此表單添加validate_group? 但是當我檢查表單是否有效時-表單始終有效,但是實體為emthy並且實體具有斷言不為空的字段,我在做什么錯呢?

實體

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;

建立表格

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']
    ));
}

和行動

        $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;
}

我嘗試行動

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

但仍然有空的$error

為什么表格是正確的? 還是如何在我的實體中使用正確的有效表單模型並在該實體中進行斷言?

添加到“ CreateUser”表單的“ cascade_validation”選項以驗證嵌套表單,並檢查是否在config.yml中指定了約束的注釋方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM