symfony 必需选项如何工作?

问题描述 投票:0回答:2

我不知道为什么,但我记得当你有一个 symfony 表单且未渲染必填字段时,

isValid
函数不会检查它们。

我的意思是它只检查小部件在您使用它时是否有值。在我当前的项目中似乎这是错误的。

是否有设置或允许执行此操作?

编辑:这是表格:

    class DemandeForm extends BaseDemandeForm
    {
  public function configure()
  {
    $this->widgetSchema['DEMANDE_DESC']         = new sfWidgetFormTextarea();
    $this->validatorSchema['DEMANDE_DESC']      = new sfValidatorString(array('required' => true),
    array("required" => str_replace('$1','Description de la demande',MessagePeer::getFrameworkMessageFromNum(7))));

    $this->validatorSchema['DEMANDE_LIB']      = new sfValidatorString(array('required' => true),
    array("required" => str_replace('$1','Libelle de la demande',MessagePeer::getFrameworkMessageFromNum(7))));

    $this->widgetSchema['TIERS_ID']         = new crfcWidgetAutocomplete();
    $this->validatorSchema['TIERS_ID']      = new sfValidatorString(array('required' => true),
    array("required" => str_replace('$1','Tiers',MessagePeer::getFrameworkMessageFromNum(7))));

    $bat_choices = $this->getOption('bat_choices');
    $this->widgetSchema['BAT_CODE']         = new sfWidgetFormChoice(array('choices' => $bat_choices , 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['BAT_CODE']      = new sfValidatorChoice(array('choices' => array_keys($bat_choices),'required' => false)
    );

    $local_choices = $this->getOption('local_choices');
    $this->widgetSchema['LOCAL_CODE']       = new sfWidgetFormChoice(array('choices' => $local_choices, 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['LOCAL_CODE']    = new sfValidatorChoice(array('choices' => array_keys($local_choices),'required' => false)
    );

    $site_choices = $this->getOption('site_choices');
    $this->widgetSchema['SITE_CODE']       = new sfWidgetFormChoice(array('choices' => $site_choices, 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['SITE_CODE']    = new sfValidatorChoice(array('choices' => array_keys($site_choices),'required' => false)
    );

    $ress = Array(""=>"") + EnumereValeurPeer::getByType('FONCTIONS',true);
    $this->widgetSchema['FONCTION_EVAL_NUMINT']         = new sfWidgetFormChoice(array('choices' => $ress , 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['FONCTION_EVAL_NUMINT']      = new sfValidatorChoice(array('choices' => array_keys($ress),'required' => false),
    array("required" => str_replace('$1','Fonction demandeur',MessagePeer::getFrameworkMessageFromNum(7))));

    $ress = Array(""=>"") + EnumereValeurPeer::getByType('CIRCONSTANCE',true);
    $this->widgetSchema['CIRCONSTANCE_EVAL_NUMINT']       = new sfWidgetFormChoice(array('choices' => $ress, 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['CIRCONSTANCE_EVAL_NUMINT']    = new sfValidatorChoice(array('choices' => array_keys($ress),'required' => false),
    array("required" => str_replace('$1','Circonstance de la demande',MessagePeer::getFrameworkMessageFromNum(7))));

    $ress = Array(""=>"") + EnumereValeurPeer::getByType('TRAVAUX',true);
    $this->widgetSchema['CATEGORIE_EVAL_NUMINT']         = new sfWidgetFormChoice(array('choices' => $ress , 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['CATEGORIE_EVAL_NUMINT']      = new sfValidatorChoice(array('choices' => array_keys($ress),'required' => false),
        array("required" => str_replace('$1','Catégorie de travaux',MessagePeer::getFrameworkMessageFromNum(7)))
    );

    $ress = Array(""=>"") + EnumereValeurPeer::getByType('QUALIFICATION',true);
    $this->widgetSchema['QUALIFICATION_EVAL_NUMINT']         = new sfWidgetFormChoice(array('choices' => $ress , 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['QUALIFICATION_EVAL_NUMINT']      = new sfValidatorChoice(array('choices' => array_keys($ress),'required' => true),
        array("required" => str_replace('$1','Qualification',MessagePeer::getFrameworkMessageFromNum(7)))
    );

    $ress = Array(""=>"") + EnumereValeurPeer::getByType('PROGRAMME',true);
    $this->widgetSchema['PROGRAMME_EVAL_NUMINT']         = new sfWidgetFormChoice(array('choices' => $ress , 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['PROGRAMME_EVAL_NUMINT']      = new sfValidatorChoice(array('choices' => array_keys($ress),'required' => true),
            array("required" => str_replace('$1','Programme',MessagePeer::getFrameworkMessageFromNum(7)))
            );

    $ress = Array(""=>"") + EnumereValeurPeer::getByType('SUPPORT',true);
    $this->widgetSchema['SUPPORT_EVAL_NUMINT']         = new sfWidgetFormChoice(array('choices' => $ress , 'expanded' => false, 'multiple' => false));
    $this->validatorSchema['SUPPORT_EVAL_NUMINT']      = new sfValidatorChoice(array('choices' => array_keys($ress),'required' => false),
    array("required" => str_replace('$1','Support',MessagePeer::getFrameworkMessageFromNum(7))));

    $this->widgetSchema['COMMENTAIRE']         = new sfWidgetFormTextarea();
    $this->validatorSchema['COMMENTAIRE']      = new sfValidatorString(array('required' => false));


    $this->widgetSchema['DEMANDE_DTDEM']                    = new myWidgetFormRichDate();
    $this->validatorSchema['DEMANDE_DTDEM']                 = new sfValidatorDate(array('required' => true, 'date_format'=>'@(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})@'),
        array('required'=> str_replace('$1','Date de la demande',MessagePeer::getFrameworkMessageFromNum(7)),
        'bad_format' => str_replace('$1','Date de la demande',MessagePeer::getFrameworkMessageFromNum(19)),
        'invalid' => str_replace('$1','Date de la demande',MessagePeer::getFrameworkMessageFromNum(19))));

    $this->widgetSchema['DEMANDE_DTFIN']                    = new myWidgetFormRichDate();
    $this->validatorSchema['DEMANDE_DTFIN']                 = new sfValidatorDate(array('required' => true, 'date_format'=>'@(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})@'),
        array('required'=> str_replace('$1','Date de fin des travax souhaitée',MessagePeer::getFrameworkMessageFromNum(7)),
        'bad_format' => str_replace('$1','Date de fin des travax souhaitée',MessagePeer::getFrameworkMessageFromNum(19)),
        'invalid' => str_replace('$1','Date de fin des travax souhaitée',MessagePeer::getFrameworkMessageFromNum(19))));

    $this->widgetSchema['DEMANDE_ACCESSIBILITE']            = new sfWidgetFormInputCheckbox();
    $this->validatorSchema['DEMANDE_ACCESSIBILITE']         = new sfValidatorBoolean(array('required' => false));

    $this->widgetSchema['DEMANDE_URGENT']                   = new sfWidgetFormInputCheckbox();
    $this->validatorSchema['DEMANDE_URGENT']                = new sfValidatorBoolean(array('required' => false));

    $this->widgetSchema['DEMANDE_INCENDIE']                 = new sfWidgetFormInputCheckbox();
    $this->validatorSchema['DEMANDE_INCENDIE']              = new sfValidatorBoolean(array('required' => false));

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


}

编辑2:

问题是:比所需的DEMANDE_DTDEM字段正常,引发必填表单字段错误,而他还没有被渲染?

表单字段不应该只在使用时才验证吗?

php symfony1 symfony-1.4 propel required
2个回答
3
投票

是的,这是正常行为。字段验证与渲染无关。 如果您不想渲染小部件,则必须显式删除小部件和验证器(使用未设置或 useFields)。

如果您定义了一个小部件和一个验证器(如您的情况),但您不调用

$form['DTDEM']->render()
,则该值将始终为空。

您了解这一点非常重要。在这种情况下,该字段是必需的,然后变成该错误,但如果不是必需的,

$form->save()
方法将始终清除数据库中对象的值!

切勿让小部件处于未渲染状态。你应该在调试时做一些 echo

$form
来查看配置了哪些小部件,如果你不想要其中一些小部件,只需在表单
configure()
方法中删除它们(正如我所说,使用 unset 或 useField)


0
投票

我想对glerendegui的答案进行补充。

他所说的在一般情况下都是正确的。但是,游戏规则正在改变。表单的选项filter_extra_fields(false) 显着改变了规则。这就是我最初的误解的原因。

因为将此选项设置为 false,当您的小部件尚未渲染时,$form->save() 不会在您的对象中设置空值。

这是一个重要的补充,因为在我继承的所有遗留代码中,此选项的使用很常见,并且没有受到挑战。

© www.soinside.com 2019 - 2024. All rights reserved.