将一个变量传递给Symfony表单 [英] Pass a variable to a Symfony Form

查看:226
本文介绍了将一个变量传递给Symfony表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 1.4和Doctrine为一所学校构建一个Web应用程序,并且我想制作一个非常简单的表单来为学生添加课程。

我的主要问题是,在下拉列表中,我只想显示学生当前没有注册的课程。



我已经在模型中有一个函数(在Student.class.php中),它返回所有没有注册学生的课程,但问题是我不知道如何将学生传递给表单的configure()。我已经尝试了几个选项,比如将它与表单的构造函数一起传递给一个全局变量或一个特殊的set方法,但它们都没有工作。



是否有任何形式将学生传递给configure()方法?



谢谢!

解决方案

<

 在你的动作中:

$ this-> ; form = new StudentCourseForm(array(),array('student_id'=> $ student_id));

在表单类中:

$ this-> getOption('student_id');


I am building a web application using Symfony 1.4 and Doctrine for a school and I want to make a very simple form to add a course to a student.

The main problem I have is that in the drop down list I only want to show the courses in which the student is currently not enrolled.

I already have a function in the model (in Student.class.php) which returns all the courses in which the student is not enrolled but the problem is I don't know how to pass the student to the configure() of the form. I have tried several options like passing it with the constructor of the form to a global variable or a special set method but none of them have worked.

Is there any form to pass the student to the configure() method?

Thanks!

解决方案

This should work for you...

In your action:

$this->form = new StudentCourseForm(array(), array('student_id' => $student_id));

In the form class:

$this->getOption('student_id');

这篇关于将一个变量传递给Symfony表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆