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

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
    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 }
    
    点赞 评论

相关推荐