0

こんにちは、仕様フォームとソース フォームの 2 つのフォームがあります。

ユーザーが仕様と仕様のソースを同時に送信できるように、2 つのフォームを 1 つに統合しています。

問題は、仕様テーブルに name というフィールドがあり、ソース テーブルに name というフィールドがあることです。したがって、フォームを作成してマージするとき、仕様名とソース名という 2 つの異なるものを参照する必要がある 2 つの名前フィールドがあります。モデル/データベースを再構築せずにこれを回避する方法はありますか?

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 に答える