Skip to content
Advertisement

CSRF Invalid because of two entities in the registration process

I have two entities and one form for the registration process.When I add csrf token it probably works for one of them, not for both.

JavaScript

The output is: “user__token” so as far as I can see it generates it with the exception that is affects only one of the two entities – User and it does not work for the other one.

When I remove the company fields everything works like a charm.

The forms are:

1.User

JavaScript

2.Company:

JavaScript

The controller where forms are being created:

JavaScript

How could I fix it?

Advertisement

Answer

This is basically a question of how to combine two entities into one form. There are other questions that address this but some are a bit old and out of date. This question is also an example of not taking advantage of what Symfony has to offer and working a bit harder than the developer has to.

Specifically, Symfony deals with csrf out of the box and requires no special configuration. You can do more stuff with it but it is easier to start with a default working implementation and then tweak if necessary. In a similar fashion, twig has a default rendering of a form. After the form is working you can then use various styling tools to make it look pretty.

Start by simplifying your EntityType classes and adding a RegisterType:

JavaScript

Now adjust the controller code and use the default form rendering:

JavaScript

At this point you should have a working form. You can then add validation and persistence as well as making the form look nice.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement