I defined a custom form as follows:
class EditOwnerProfileType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add("user", new UserType()) ->add("dog", new DogType()) ->add('save', 'submit'); } public function getName() { return 'edit_owner'; } }
I want to create this form and initialize it with some data:
$user = new User(); $user->setLatitude(1.1) ->setLongitude(2.2) ->setAddress("custom address"); $dog = new Dog(); $dog->setDogName("Bruno") ->setDogSize("small") ->setDogBreed("Bulldog"); $formData = array( "user" => $user, "dog" => $dog ); $form = $this->createForm(new EditOwnerProfileType(), $formData, array("csrf_protection" => false))->handleRequest($request);
DogType and UserType have NotBlank restrictions
Every time I want to check the data, it always gives an error for each field as follows:
"errors": { "user": { "latitude": [ "This value should not be blank." ], "longitude": [ "This value should not be blank." ], "address": [ "This value should not be blank." ] }, "dog": { "dogName": [ "This value should not be blank." ], "dogSize": [ "This value should not be blank." ], "dogBreed": [ "This value should not be blank." ] },
It is not assumed that I initialize all the values? So, if the user does not pass any value for this field, is it initialized with the values ββthat I defined? What is the correct way to initialize values ββin the EditOwnerProfileType form?
EDIT: I tried changing the form creation (for testing only), but did not work.
$form = $this->createForm(new EditOwnerProfileType(), $formData, array("csrf_protection" => false)); $form->setData($formData); $form->handleRequest($request);
EDIT2: enable DogType and UserType code
class DogType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('dogSize') ->add('dogBreed') ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Dog', )); } public function getName() { return 'dog_type'; } } class UserType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('latitude') ->add('longitude') ->add('address') ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\User', )); } public function getName() { return 'user_type'; } }
I am using Symfony 2.7.9