简体   繁体   English

向Symfony2中的表单添加变量

[英]Add variables to a form in Symfony2

I'm using a Symfony 2 in a project, and I need to have dependent selects in my form. 我在项目中使用Symfony 2,并且需要在表单中具有从属选择。 Currently I'm following this tutorial showmethecode.es and its working. 目前,我正在关注showmethecode.es教程及其工作。

But I'm also using options groups in the selects, and I really need to add some extra variables and some logic in the client side. 但是我还在selects中使用了选项组,我确实需要在客户端添加一些额外的变量和一些逻辑。

My question is, how can I add variables to a form (in the client side), and join them in a Type class. 我的问题是,如何将变量添加到表单中(在客户端),并将其加入Type类中。

Either you read up on Custom Form Field types and Data transformers: 您可以阅读自定义表单字段类型和数据转换器:

Or you create the form yourself and get the data from the request in the send action. 或者,您自己创建表单,并在send操作中从请求中获取数据。 You should however create a CSRF Token when creating the form and check it when submitting the form: 但是,您应该在创建表单时创建CSRF令牌,并在提交表单时进行检查:

$this->get('security.csrf.token_manager');
//Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider
$token = $csrf->getToken("bundlenamespace_formname");

And validate it like so: 然后像这样验证它:

$this->get('security.csrf.token_manager')->isTokenValid($request->get('_token', ''));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM