0

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.

Steve Adams
  • 2,812
  • 22
  • 29
  • What exactly are you trying to do? – cwallenpoole Sep 30 '11 at 19:42
  • 2
    Are you aware that doctrine (and maybe propel too) let you generate forms from the model? So actually, you don't even have to write a component... – greg0ire Sep 30 '11 at 19:56
  • @greg0ire I wasn't aware of this. Wow. So I suppose that is best practice in cases like these? – Steve Adams Sep 30 '11 at 20:45
  • @cwallenpoole 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. – Steve Adams Sep 30 '11 at 20:47

2 Answers2

4

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.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • Are you aware of any documentation on this method? I'm taking a quick look and not yet finding anything which explains usage in my case. If not, that's cool - Thanks for the answer! – Steve Adams Sep 30 '11 at 20:51
  • [This page](http://www.symfony-project.org/forms/1_4/en/11-Doctrine-Integration) explains how to generate forms with doctrine, and how to customize them in children classes. – greg0ire Sep 30 '11 at 21:01
  • At a glance it appears that I need to know what the fields will be. With this project, I don't actually know what will be coming out of the model (It may contain 3 elements called name, height and weight, or it may have 4 called age, sex, phone number, email address, etc), so I can't prepare base forms for the eventual elements. Or am I missing something? – Steve Adams Sep 30 '11 at 21:09
  • If the fields can be anything, then you may need something even more abstract, like a EAV model. When you say that you don't know the fields, do you mean that the advertisers are going to build the forms themselves, using... a form? In that case, my answer is probably not the solution for you... unless you manage to generate one table for each model when an advertiser submits the form. – greg0ire Sep 30 '11 at 21:21
  • The information about the forms is entered into our database via CSV files - What our advertisers send us. It's entered manually since it's done so infrequently. Allowing updates using forms would be very cool, but a little out of the scope for this deadline. – Steve Adams Sep 30 '11 at 21:56
  • Ok then stick to what you were trying to do. BTW, why don't you think it should work? It looks perfectly fine to me. – greg0ire Sep 30 '11 at 22:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3943/discussion-between-steve-adams-and-greg0ire) – Steve Adams Sep 30 '11 at 22:30
1

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.

Community
  • 1
  • 1
greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • Currently all I need to do is gather the data and send it to an in-house service which validates it using collected data both from our own sites and our advertisers. Upon getting a response from the service, I can then pass the user through to the proper path. – Steve Adams Sep 30 '11 at 21:50
  • Then I think what you started to do is all right... all you need to decide is whether or not tu use a NoSQL system to store your FormElement objects... – greg0ire Sep 30 '11 at 22:01