如何在symfony 1.4中正确设置条件验证器?

时间:2011-01-19 02:32:12

标签: forms symfony1 conditional validation

由于某种原因,我的表单中的字段未经过验证。也许有人可以指出我的代码中的任何错误或缺少项目。我试图按照这里的例子:http://www.symfony-project.org/cookbook/1_2/en/conditional-validator

感谢任何帮助!

我有以下表单类:

    <?php

/**
 * dnc_btns form.
 *
 * @package    NMPlus
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class dnc_btnsForm extends Basednc_btnsForm
{
    public function configure()
  {
    $this->useFields(array('btn', 'btn_low', 'btn_high', 'company', 'source'));


    $this->setValidator('company', new sfValidatorPass());
    $this->setValidator('source', new sfValidatorPass());
    $this->validatorSchema->setPostValidator(
          new sfValidatorCallback(array('callback' => array($this, 'checkBtnType')))
        );

  }

    public function checkBtnType($validator, $values)
        {
                if (!empty($values['btn']))
                    {
                        $this->setValidator('btn', new sfValidatorBtn());
                        $this->setValidator('btn_low', new sfValidatorPass());
                        $this->setValidator('btn_high', new sfValidatorPass());
                    }

                if(empty($values['btn']))
                    {
                        $this->setValidator('btn_low', new sfValidatorBtn());
                        $this->setValidator('btn_high', new sfValidatorBtn());
                        $this->mergePostValidator(
                                    new sfValidatorSchemaCompare('btn_low', sfValidatorSchemaCompare::LESS_THAN, 'btn_high', array(),
                                    array('invalid' => 'The first BTN must be lower than the second.')));

                    }

                    return $values;
        }

}

这是我的自定义验证器:

<?php

class sfValidatorBtn extends sfValidatorAnd
{

    public function __construct()
    {
        parent::__construct();
        $this->setValidators();
    }

    public function setValidators()
    {


        //Btn should be required, not trimmed, and min and max length set.
        $this->addValidator(
                new sfValidatorString(
                        array(
                            'required' => true,
                            'trim' => false,
                            'min_length' => 10,
                            'max_length' => 10,
                            ),
                        array(
                            'invalid' => 'BTN must be 10 digits.'
                            )
                        )
                );

        // Adapted from http://stackoverflow.com/questions/1964399/validation-for-a-10-digit-phone-number
        $this->addValidator(
                new sfValidatorRegex(
                        array( 'pattern' => '/^([1-9]\d*)/' ),
                        array( 'invalid' => 'BTN may not start with a 0.')
                        )
                );

        //Checking for existance of given btn in database
        $this->addValidator(
                new sfValidatorDoctrineUnique(
                        array( 'model' => 'dnc_btns', 'column' => 'btn' ),
                        array('invalid' => 'This BTN is already in the database.')
                        )
                );
    }
}

2 个答案:

答案 0 :(得分:1)

checkBtnType()方法应该执行验证,而不是添加新的验证器。

如果一切正常,您的方法应返回值,否则抛出sfValidatorError异常。

您似乎没有按照粘贴的链接进行教程。

答案 1 :(得分:0)

我没有测试过(也许我错了),我认为你可以通过手动创建验证对象来在checkBtnType中执行整个验证:

$validator = new sfValidatorBtn();
$validator->doClean($values['btn']);

希望有所帮助。