2

I am submitting a form with a lot of fields and trying to validate it with handleRequest($request) as it is shown in the Symfony documentation. My entity is very big and has a lot of relations with other entities. handleRequest($request) is validating each form field submitted and checking for errors.

The problem found is while submitting an id of a related entity of my main entity (in example a person of an office), handleRequest will internally get all objects of the related entity (the full table of the related entity, all persons) and hydrating them as objects.

I think it should just check if the submitted id exists in the other table, get that related entity object and check it for errors (instead of getting all the related table).

If you check and debug the source code of Symfony2 handleRequest, you may easily spot the same problem at this lines:

Form/Form.php

// Normalize data to unified representation
$normData = $this->viewToNorm($viewData);
$value = $transformers[$i]->reverseTransform($value);

How can I still validate the form without dealing with this issue which makes it insanely slow to validate a form with handleRequest($request)?

If I don't use handleRequest to validate it, which automatically add the errors to my form for each field, how could I manually validate each field and later add the errors to my form for each field and show them in the next view?

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
xtrm
  • 966
  • 9
  • 22
  • can you provide more on how you solved this issue? my $form->handleRequest($request) is taking over 30 seconds to process. I think it has to do with one of my entity's properties being tied to another entity. My entity's child prop IS an entity (of different class). Whatever symfony is doing to map these entities together is taking too damn long. – TrieuNomad Mar 30 '16 at 23:16
  • Both in the main form and also in the form of all related entities you must always specify the variable type for all fields. http://stackoverflow.com/questions/7913086/how-to-set-default-value-for-form-field-in-symfony2 Although it is slow, you may also debug handleRequest and check the loop where it is checking all fields, when it finds an entity, it will check all the fields of that entity entering in another loop, and so on. – xtrm Aug 03 '16 at 17:46

2 Answers2

1

This question is a little vague, and the answer very much depends on your specific form. Please post the form definition that is giving you the hardest time.

Check to make sure that you are not EAGER fetching associations here.

handleRequest() is going to take the request object and construct the model that your form describes, as your form defined it to.

If the objects are required in order to display data on your initial form to the user, or to validate the data on submit, the "entity" field type will fetch all of the objects you told it to in its definition. If you are displaying a big select list, for example, all of this data is needed.

I had a similar problem in the past and it was because I was using a lot of choice fields that were being used as a series of multiple select checkboxes. My bottleneck was actually in the twig layer while rendering out the thousands of checkboxes I had stored as separate entities.

I switched from a set of checkboxes to a single multi-select box and it increased my speed significantly.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Chris Tickner
  • 2,008
  • 2
  • 20
  • 24
  • sorry for the lack of informatiom and ty so much for the answer, Im not at computer right now but I will update my question with full details in some hours, I think u may spotted and solved it but Ill test and report it – xtrm Mar 12 '14 at 19:46
  • ty so much for the answer, you were right. In order to avoid handleRequest to find all related entities and improve speed, the entity type must be specified in the form type. Ill update the post later with an example. – xtrm Mar 14 '14 at 09:36
0

in my case, after having a similar behaviour I detected that it was a problem in my xdebug configuration. By editing php.ini and disabling xdebug I found that everything went much faster. It can be interesting to do this check when all else fails. I'm leaving this message here in case it might be of help to someone else.

Hokusai
  • 2,219
  • 1
  • 21
  • 22