2010-11-22 53 views
1

所有内容都在标题中。用URL参数预填一个窗体小部件(symfony)

我得到的URL PARAM:

$log = $request->getParameter('logement'); 

Widget的声明:

$this->widgetSchema['logement'] = new sfWidgetFormInputText(); 

而且我通过它的形式预填我的窗口小部件 'logement':

$this->form = new bailForm(array('logement' => $log)); 

我已经在symfony的doc中读过它,但是当我这样做时,我有这个错误:

The "BailForm" form only accepts a "Bail" object. 

我已经尝试了许多在互联网上找到的东西,但没有人工作。

EDIT

的ORM是教义 “Logement” 为 “支架”

EDIT的属性2

我曾尝试:

$log = $request->getParameter('logement'); 

$this->form = new bailForm(null, array('logement' => $log)); 

我没有错误,但我的小部件“logement”未填充...

回答

3

一两种方法:

1.如果您想验证Logement

$form = new BailForm(); //BailForm must have Logement validator set 
$form->bind(array('logement' => $log) + $otherRequestParameters); 
$form->updateObject(); //or save 

2。如果你只是想Logement对象

$bail = new Bail(); 
$bail->Logement = $log; 
$form = new BailForm($bail); 
0

基于模型类自动生成的表单(在这种情况下,BailForm对于Bail)属于sfFormObject类型,因此只接受与模型类相对应的类型参数。

一个天真的解决方案是为类型BailForm声明一个自定义构造函数,它将一个数组作为单个参数(或一个数组和一个类型为Bail的对象)。

然而,这并不是很好的做法,因为模型表单只能用于模型类。这logement参数 - 它对于Bail对象有什么意义?也许如果你问自己这个问题,你可以想出一个更合适的设计,可能包含logement作为Bail的一个属性。

1

您的表单是一个推动或学说形式,构造函数的第一个参数必须是链接的对象实例。试试这个:

$this->form = new bailForm(null, array('logement' => $log)); 
0
class QuestionsForm extends BaseForm 
{ 

    private static $email; 


    public static function setEmail($set) { self::$email = $set; } 
    public static function getEmail() { return self::$email; } 

    public function configure() 
    { 

     $this->setDefault('email', self::$email); 
     //$this->setDefault('email', 'testemail'); 

     //rest of the form setup code 
    } 

} 

这里设定的是动作类

class questionsActions extends sfActions 
{ 
    public function executeIndex(sfWebRequest $request) 
    { 

    $this->email = $this->getRequestParameter('email'); 

    QuestionsForm::setEmail($this->email); 
    //die(QuestionsForm::getEmail()); 
    $f = new QuestionsForm(); 

    $this->form = $f; 
相关问题