0

嗨,我有两种表格,规格表格和来源表格。

我将这两个表单合并为一个,以便用户可以同时提交规范和规范的来源。

问题是规范表有一个名为 name 的字段,而源表有一个名为 name 的字段。因此,在创建表单和合并时,我有两个名称字段应该引用两个不同的东西,规范名称和源名称。有什么方法可以在不重构模型/数据库的情况下解决这个问题?

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>
4

1 回答 1

3

你可以试试这段代码:

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

// merge
$newsLinkForm->mergeForm($sourceForm);
于 2010-09-23T09:33:54.393 回答