0
我有兩個entoty用戶和位置,我創建兩個實體的模型,併爲此模型創建表單併爲此表單添加validate_group?但是,我檢查表格是有效的 - 表格總是有效的,但實體是emthy和實體已斷言不是空白字段,我做錯了什麼?Symfony表單驗證實體
實體
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
爲什麼表單有效是真的?或者我的實體的有效表單模型有多正確並在此實體中聲明?