OK, I googled this hard, but everything I find talks about Symfony forms in context of regular Symfony form processing (e.g. form_widget(), creating FormType class, etc.). I have many such forms in my Symfony project, they work great.
BUT:
I also have some pretty complex AJAX forms that I would like to build manually (using plain old HTML and JS). I do still want to utilize Symfony's Form validation capabilities and CSRF protection. However, for some reason I can't get CSRF working when using isValid() for manually created forms.
This is an example of what I am trying to accomplish:
In my view controller I set _token:
$_token = $this->get('form.csrf_provider')->generateCsrfToken('form');
In my view (manually created form) (getting _token from my view controller):
<html>
<form method="post">
<input type="hidden" name="form[_token]" value="{{ _token }}">
<input type="hidden" name="form[id]" value="1">
<input type="submit" value="Submit">
</form>
</html>
In my action controller (when form submitted, I am TRYING to do the following):
//Create form (for validation purposes)
$form = $this->get('form.factory')
->createBuilder('form', array('id' => $request->get('id')))
->add('id', 'hidden')
->getForm();
//Bind form
$form->bind($request)
//Validate form
if($form->isValid()) {
//... save data
}
//Return response...
For some reason I can't get isValid() working, I suspect that my _token is thing not properly used, but I am out of ideas why. Have anyone actually made manually forms work with Symfony components? Does anyone have any suggestions on how to make this work?
Basically, what I want to accomplish is:
Manually create HTML form (with CSFR protection and without TWIG form widget functions)
Use Symfony's form functionality to validate that form
Thank you.