어떤 이유로 든 내 양식의 입력란에 대한 유효성을 검사하지 않습니다. 아마도 누군가 내 코드에서 오류나 누락 된 항목을 지적 할 수 있습니다. 여기에있는 예를 따르려고했습니다. http://www.symfony-project.org/cookbook/1_2/en/conditional-validator심포니 1.4에서 조건부 검사기를 올바르게 설정하려면 어떻게해야합니까?
도움을 주시면 감사하겠습니다. 여기
<?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;
}
}
그리고 내 사용자 지정 유효성 검사기입니다 : :이 checkBtnType() 메소드는 다음 오히려 유효성 검사를 수행 새 검사기를 추가해야
<?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.')
)
);
}
}
튜토리얼을 시작점으로 사용했습니다. 내 목적에 어긋나있어. checkBtnType() 메서드가 유효성 검사를 수행해야하는 경우 코드의 해당 부분을 사용자 지정 유효성 검사기에 포함시키는 것이 더 좋지 않습니까? 또한 기존의 유효성 검사기를 구현하는 방법이있어서 다시 작성하지 않아도됩니까? 의견을 보내 주셔서 감사합니다. –
물론 사용자 정의 유효성 검사기를 구현하는 것이 좋습니다. 너를 막는 무엇? 기존의 유효성 검사기를 확장하여 다른 유효성 검사기 클래스를 확장하는 클래스를 생성하여 재사용 할 수 있습니다. –