Initialize form data of a special type

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

0
source share
1 answer

I am answering my question, the problem was ... I misunderstood the Symfony form component.

I can initialize the form:

 $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)) 

At this point, the form is correctly initialized, and all fields have the correct values.

Passing one parameter in the request edit_owner[dog][name]=othername and executing:

 $form->handleRequest($request); 

I expected to change only edit_owner[dog][name] (and the rest of the fields retain the default value), but I noticed that the rest of the form fields are processed and parsed as blank values ​​and replaced with my "initial" values. "

This was a problem, and Symfony does just that. I am making this answer, so it might be useful to someone else.

0
source

Source: https://habr.com/ru/post/952196/


All Articles