[英]Symfony form validate entity
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? 我有两个实体用户和位置,并创建了具有两个实体的模型,并为此模型创建表单,并为此表单添加validate_group? 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 ?
但是当我检查表单是否有效时-表单始终有效,但是实体为emthy并且实体具有断言不为空的字段,我在做什么错呢?
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
但仍然有空的
$error
Why form is valid true ? 为什么表格是正确的? or how correct valid form model with my entities and assert in this entities ?
还是如何在我的实体中使用正确的有效表单模型并在该实体中进行断言?
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 添加到“ CreateUser”表单的“ cascade_validation”选项以验证嵌套表单,并检查是否在config.yml中指定了约束的注释方法
# app/config/config.yml
framework:
validation: { enable_annotations: true }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.