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 ?