开发者

Prefill a form's widget with URL parameter (symfony)

All is in the title.

I get the URL param :

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

Widget's statement :

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

And I pass it in the f开发者_运维问答orm to prefill my widget 'logement' :

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

I have read it in symfony's doc, but, when I do this, I have this error :

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

I have already tried many things found on Internet but, no one works.

EDIT

The ORM is Doctrine "Logement" is an attribute of "Bail"

EDIT 2

I have tried :

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

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

I don't have error, but my widget "logement" isn't filled...


One of two ways:

1. If you want to validate Logement

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

2. If you just want Logement set on the object

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


Your form is a propel or doctrine form, the first parameter of the constructor has to be a linked object instance. Try this:

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


The forms that are auto-generated based on model classes (in this case, BailForm for Bail), are of type sfFormObject, and thus accept only parameters of type corresponding to the model class.

A naive solution is to declare a custom constructor for type BailForm that takes an array as a single parameter (or an array and an object of type Bail).

This would not be very good practice however, as model forms are designed to work with model classes only. This logement parameter - what is its significance with respect to the Bail object? Maybe if you ask yourself that question, you can come up with a more suitable design that probably incorporates the logement as an attribute of Bail.


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
  }

}

Here is the actions class

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜