如何使用不同的模板持久化symfony表单验证错误

时间:2013-09-12 06:37:12

标签: symfony1 symfony-1.4

我需要在扩展sfForm的表单中显示验证错误。问题是当我发送无效表单时,我的表单上没有显示验证错误。 这是我的表单:

class FinanceStatementOfWorkForm extends sfForm
{

  public function configure()
  {
    $this->widgetSchema->setNameFormat('master_groups[%s]');
    $finance_statement_id = $this->getOption('finance_statement_id');
    $choices              = Doctrine::getTable('FinanceTask')->getGroups($finance_statement_id);

    $this->widgetSchema['master_start'] = new sfWidgetFormChoice(
      array(
        'choices' => $choices
      )
    );
    $this->setDefault('master_start', Doctrine::getTable('FinanceTask')->getActiveMaster($finance_statement_id, $type = 'start'));
    $this->validatorSchema['master_start'] = new sfValidatorChoice(array(
      'choices'=> array_keys($choices)
    ));
    $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(array('callback'=>array($this, 'checkGroups')))
    );

    $this->widgetSchema['master_end'] = new sfWidgetFormChoice(
      array(
        'choices' => $choices
      )
    );
    $this->setDefault('master_end', Doctrine::getTable('FinanceTask')->getActiveMaster($finance_statement_id, $type = 'end'));
    $this->validatorSchema['master_end'] = new sfValidatorChoice(array(
      'choices'=> array_keys($choices)
    ));
    $this->disableCSRFProtection();

  }

  public function checkGroups($validator, $values)
  {
    if ($values['master_start'] == $values['master_end'])
      throw new sfValidatorError($validator, 'Could not be equal with master end group');
    else
      return $values;
  }

}

我的模板中的下一步:

<fieldset>
  <legend><?php echo __('Set Master Groups') ?></legend>
  <form action="<?php echo url_for('@finance_projects\saveMasterGroups') ?>" method="post">
    <?php echo $form ?>
    <div class="w-box-b">
      <div class="button-white button-save">
        <span><button type="submit"><span><?php echo __('Save') ?></span></button></span>
      </div>
    </div>
  </form>
</fieldset>

然后我在操作中处理表单:

  public function executeSaveMasterGroups(sfWebRequest $request)
  {
    $this->form = new FinanceStatementOfWorkForm(array(), array(
      'finance_statement_id'=>$this->finance_statement->id
    ));

    $this->processSaveMasterGroupsForm($request, $this->form);
//    $this->executeIndex($request);
//    $this->setTemplate('index');

  }

  public function processSaveMasterGroupsForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      Doctrine::getTable('FinanceTask')->updateGroups($request->getParameter('master_groups'));
    }
    else
      $this->getUser()->setFlashFormError(false);

  }

如果我设置了模板,我会看到一个破损的模板(缺少我的executeIndex中的一些变量)和我的表单,但带有验证错误。如果之前执行索引或只是注释掉那两行,我只得到flash消息。如何解决?

1 个答案:

答案 0 :(得分:1)

找到解决方案,更注重定位动作调用。

  public function executeSaveMasterGroups(sfWebRequest $request)
  {
    $this->executeIndex($request);
    $this->processSaveMasterGroupsForm($request, $this->form);
    $this->setTemplate('index');

  }

  public function processSaveMasterGroupsForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      Doctrine::getTable('FinanceTask')->updateGroups($request->getParameter('master_groups'));
    }
    else
      $this->getUser()->setFlashFormError(false);
    $this->form  = $form;
  }