Creating forms manually in Symfony2, but still use its CSRF and isValid() functionalily

后端 未结 4 1379
南旧
南旧 2020-12-29 07:22

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

4条回答
  •  猫巷女王i
    2020-12-29 07:57

    I think you're mismatching the intention here (argument passed to your CSRF provider). I tried generating form as you wrote above and break-pointed the generation of token. The value was unknown.

    So, try passing unknown instead of form to your generateCsrfToken call and hopefully it should work. ;)

    EDIT:

    I have just finished some digging and it now does make perfect sense.

    Look at the class FormTypeCsrfExtension. Apparently, it's the default extension used for CSRF token protection. On the line #80 (might not be this one exactly in your case) there is method setDefaultOptions that is usually overridden in your form types. Anyhow, there is a default options called intention that has a value of unknown ==> the one we are seeing here.

    My guess is that you could easily override this option in your own form type just by passing intention and setting your own value (just as you would pass csrf_protection => false when you would want to disable CSRF protection altogether).

提交回复
热议问题