2010-09-23 168 views
0

嗨,我有两种形式,一个规范表单和一个源表单。Symfony合并两个具有相同名称字段的表单

我正在将两种形式合并为一种,以便用户可以同时提交规范和规范的来源。

问题是规格表有一个名为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> 

回答

3

你可以试试这段代码:

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

// merge 
$newsLinkForm->mergeForm($sourceForm); 
+0

嗯,我尝试这样做,得到错误“字段必须sfWidget的一个实例。” – Marc 2010-09-23 12:27:48

+0

@Marc:奇怪... $ newsLinkForm ['name']的类是什么? – greg0ire 2010-09-23 12:48:53

+0

所以最终成为sfFormInput类。 getWidget('name')将获得小部件类。我最终在合并的源表单中使用以下名称将其重命名为source_name:$ this-> setWidget('source_name',$ this-> getWidget('name')); 未设置($ this ['name']); – Marc 2010-09-23 14:53:49

相关问题