2010-11-22 55 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; 
相關問題