开发者

Can this possibly work to generate forms in Symfony 1.4?

I'm currently attempting to generate form elements in Symfony based on the application's model. I know the code below is invalid, but I'm hoping it shows what I'm trying to do. I've really hit a wall with it... I can't seem to wrap my head around how I'd utilize the form library to accomplish this.

$formElementArray = FormElementsTable::getInstance()->getElementList($advertiserID);
$formElements = array();
foreach ($formElementArray as $formElement)
{
  $formElements['name'] . => new sfWidgetForm.$formElements['type'] . (array('label' => $formElements['label'])),
}

$this->setWidgets(array($formElements));

My concern right now is that this isn't possible and I should abandon the idea, perhaps write a symfony component which produces forms from the model. That would actually be fairly simple, but I suppose I'd just like to stay as deep within the framework as possible for several reasons.

Thanks for any input.

edit: A bit more information...

The purpose is to be able to populate a form with whatever elements appear in a certain row of the database. For example, perhaps advertiserX needs a name, sex, and hair colour field, while advertiserY needs a name, sex, weight, and eye colour field. I need to be able to produce those on the fly, ideally with symfony's form features available.

Solu开发者_如何学Pythontion

This is how the form is configured...

public function configure()
{

  $elements = Doctrine_Core::getTable('FormFields')->getElementsById(0);

  foreach ( $elements as $element )
  {
    $widgetName = $this->getWidgetName($element);
    $args = $this->getConstructorArguments($element);
    $widgets[$element->name] = new $widgetName($args);

    $validatorName = 'sfValidatorString';
    $validators[$element->name] = new $validatorName(array('required' => false));
  }


  $this->setWidgets($widgets);

  $this->setValidators($validators);

}

Simple, right? I shouldn't have over thought it so much.


It is best pratice to generate forms from your model, because if you change your models, you'll probably want to change your forms too. Doctrine and Propel both support this, so don't reinvent the square wheel : use what already exists ;)

UPDATE

In your case, maybe the model can be the same for all the advertisers, and all you need to do is extend the form to make the small variations appear in the forms. Your classes will call parent methods of the generated form, and slightly change widgets and validators.


If you intend to store the data sent when filling the user form (not the form form), then I think you should use a NoSQL system because your schema is dynamic (it changes when the advertiser changes). Read this blog post to learn more about them. Also read this SO post to learn what can be used with symfony.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜