0

Here is the situation, I have a form:

public function buildForm(FormBuilderInterface $builder, array $options) {

    foreach ($this->attributeGroupAttributes as $attribute) {
        $builder->add('attribute-' . $attribute->getId(), $attribute->getType(), array(
            'label' => $attribute->getName(),
        ));
    }
}.

This form I'm using to validate data added dynamically to another form which was submitted.

This is the process how I do it:

  1. I extract the data from the previous form.
  2. I pass this data to another Action.
  3. Here I get the attributeGroup.
  4. I pass the Attributes in that group into the FormType and create a new form.
  5. After which I put the data into the form via:

    $form_validate->submit($normalizedData);
    

This is the controller:

    public function createAction($formData) {

    $itemService = $this->get('app.item');
    $attributeGroupService = $this->get('app.attribute_group');
    $attributeService = $this->get('app.attribute');

    $attributeGroup = $attributeGroupService->getById($formData['attributeGroup']);
    $attributeGroupAttributes = $attributeGroup->getAttributes();
    $form_validate = $this->createForm(new \AppBundle\Form\ItemValidationType($attributeGroupAttributes));

    $normalizedData = $itemService->normalizeDataForForm($formData);

    $form_validate->submit($normalizedData);

    if($form_validate->isValid()){
        dump($form_validate->getData());
        die();
    }
    die();
}

The error I get is:

-errors: array:1 [▼
0 => FormError {#855 ▼
  -message: "The CSRF token is invalid. Please try to resubmit the form."
  #messageTemplate: "The CSRF token is invalid. Please try to resubmit the form."
  #messageParameters: []
  #messagePluralization: null
  -cause: null
  -origin: Form {#836}
}

And that way ofcourse $form_validate->isValid() is false.

0 Answers0