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.
Solution
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.