views:

100

answers:

1

Hi I have two forms, a Specification form and a Source form.

I'm merging the two forms into one so that users can submit a specification and the source of the specification at the same time.

The problem is that the specification table has a field called name and the source table has a field called name. So, when creating the forms and merging, I have two name fields that should refer to two different things, the specification name and the source name. Any way to get around this without restructuring the model/database?

class NewsLinkForm extends BaseNewsLinkForm
{
  public function configure()
  {
    unset($this['id']);

    $link = new SourceForm();
    $this->mergeForm($link);

    $this->useFields(array('name', 'source_url'));

    $this->setValidators(array(
      'source_url'    => new sfValidatorUrl(),
    ));

    $this->validatorSchema->setOption('allow_extra_fields', true);
  }
}

class SourceForm extends BaseLimelightForm
{
  public function configure()
  {
    $this->useFields(array('name'));

    $this->setWidgets(array(
      'name'   => new sfWidgetFormInputText(array(),
        array(
          'class'     => 'source_name rnd_3',
          'maxlength' => 50,
          'data-searchahead' => url_for('populate_sources_ac'),
          'data-searchloaded' => '0'
        )),
    ));

    $this->setValidators(array(
      'name'          => new sfValidatorString(array('trim' => true, 'required' => true, 'min_length' => 3, 'max_length' => 50)),
    ));

    $this->widgetSchema->setNameFormat('source[%s]');
  }
}

<h5>add specification</h5>
    <div class="item">
      <?php echo $specificationForm['name']->renderLabel() ?>
      <?php echo $specificationForm['name']->render(array('data-searchahead' => url_for('populate_lime_specifications_ac'), 'data-searchloaded' => '0')) ?>
    </div>
    <div class="item">
      <?php echo $specificationForm['content']->renderLabel() ?>
      <?php echo $specificationForm['content']->render(array('data-searchahead' => url_for('populate_specifications_ac'), 'data-searchloaded' => '0')) ?>
    </div>
    <div class="clear"></div>
    <div class="item">
      <?php echo $specificationForm['name']->renderLabel() ?>
      <?php echo $specificationForm['name']->render() ?>
    </div>
    <div class="item">
      <?php echo $specificationForm['source_url']->renderLabel() ?>
      <?php echo $specificationForm['source_url']->render() ?>
    </div>
+2  A: 

Hello,

You could try this piece of code:

// rename the name field of the first form
$sourceForm->setWidget('source_name', $sourceForm->getWidget('name')); 
unset($this['name']);

// merge
$newsLinkForm->mergeForm($sourceForm);
greg0ire
Hmm I tried this and get the error 'A field must be an instance of sfWidget.'
Marc
@Marc: strange... what is the class of $newsLinkForm['name']?
greg0ire
So that ends up being of class sfFormInput. getWidget('name') will get the widget class. I ended up using the following in the source form that is getting merged to rename the name to source_name: $this->setWidget('source_name', $this->getWidget('name')); unset($this['name']);
Marc
If you edit your answer I'll accept it and upvote you. Thanks for the setWidget tip to rename it! Should have thought about that.
Marc
@Marc: glad you found out, post edited.
greg0ire