1

我在字段 postalCode 上的 validate/confirm.yml 中进行了验证检查:

 postalCode:  
  required: yes
  required_msg: ENTER_VALID_POSTALCODE
  validators: numberValidator 

numberValidator:
  class: sfNumberValidator
  nan_error:    NOT_A_NUMBER_ERROR

出于某种原因,我想在我的操作文件中移动类 sfNumberValidator 的逻辑或相同功能。

喜欢

class registerActions extends sfActions
{
    public function validateConfirm() {
            ....
            ...
        $postalCode = $this - > getRequestParameter('postalCode');

        if (postalCode != '') {

            //Do the same kind of validation like sfNumberValidator does 

        }

    }
....
}
4

1 回答 1

1

您必须创建sfNumberValidator实例,然后调用该execute方法。

像这样的东西:

if ($postalCode != '') {

  $validator = new sfNumberValidator($this->getContext(), array(
           'nan_error' => 'Not a number',
           'min'       => 10000,
           'max'       => 99999
  ));
  $error = null;

  if(!$validator->execute($potalCode, $error)){
      //Here $error has the message you want to show the user
  }


}

PS:最小值和最大值只是一个建议,但您可能应该为您的邮政编码情况使用更明确的东西

于 2013-08-29T22:58:45.797 回答