I have a user creation form (on top of Symfony Form component), it's used on some different pages and is loaded via AJAX.
An initial request contains information we already know about a user-to-be, e.g. if form is loaded from a manager creation page for the "X" company, we already know that a user has a role = "manager" and company = "X". And the corresponding form fields must be removed/hidden from the form and underlying user object must be provided with these parameters (a role field must be set to "manager", a company field must be set to "X").
How can it be achieved?
From what I understand you can use the answer for this question : https://stackoverflow.com/a/18792299/4098335
Basically, after calling createForm(), you have to call setData() for the desired field.
If this doesn't work, maybe try overriding the constructor for your AbstractType class. But it's a bit "hacky" and clearly not the easiest way... Example here :
Then, in your controller, pass the "manager" entity when you instantiate YourUserType :
Note: in the example I put the 'read_only' option to true, that way you'll 'avoid' the user to select another value. You can hide the field anyway later in front end, but still you should check the value later when dealing with the form request.