我在Zend 2.2中创建了一个自定义验证器。但这种行为很奇怪。 这是我的表格
namespace module\Form;
use Zend\Form\Form;
use Zend\Form\Element;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
class HistoryForm extends Form {
public function __construct($name = null){
parent::__construct('History');
$filter = new InputFilter('ReportingCurrencyHistoryFilter');
// creating a form element.
$efrm = new Element\Text('effective_from');
$efrm->setLabel("Effective From");
$efrm->setAttributes(array(
'id' => 'effective_from', 'required'=>'required', 'maxlength'=>10, 'size'=>10
));
// adding the form element to the form
$this->add($efrm);
// Effective Till
// creating a form field
$etill = new Element\Text('effective_till');
$etill->setLabel("Reporting Currency 'Effective Till'");
$etill->setAttributes(array(
'id' => 'effective_till', 'maxlength'=>10, 'size'=>10
));
//adding an element to the form
$this->add($etill);
// creating new filter for the field
$flt_eftill = new Input('effective_till');
// assigning the custom validator and attaching to the filter
$etill_history_conflict = new \APP\CustomValidators\HistoryConflict();
$flt_eftill->getFilterChain()->attach($etill_history_conflict);
// add the filter chain to filter object
$filter->add($flt_eftill);
// setting the filter as input filter for the form
$this->setInputFilter($filter);
// Submit button
$submit = new Element\Submit('submit');
$submit->setAttribute('value', 'Submit');
$this->add($submit);
}
}
这是我的自定义验证器类
namespace APP\CustomValidators;
use Zend\Validator\AbstractValidator;
class HistoryConflict extends AbstractValidator{
const ERROR_NO_DATA = 'nodata';
const ERROR_CONFLICT = 'conflict';
protected $messageTemplates = array(
self::ERROR_NO_DATA => "No data entered. this is a mandatory field",
self::ERROR_CONFLICT => "This data has conflict with previous history",
);
public function __construct($options = null)
{
parent::__construct($options);
}
public function isValid($value)
{
$this->setValue($value);
$valid = true;
if(!trim($value))
{
$this->error(self::ERROR_NO_DATA);
$valid = false;
}
if(!is_array($value)) //
{
$this->error(self::ERROR_CONFLICT);
//echo $value . '<br />';
$valid = false;
}
return $valid;
}
}
我故意在我的验证器中执行is_array检查以使验证失败,以查看自定义验证器中指定的错误消息。但是当我填写并提交表格时,我会
值是必需的,不能为空
而不是
此数据与之前的历史记录(在自定义验证程序中指定)冲突
这是我的控制器部分
public function insertAction(){
if($this->request->isPost())
{
$form = $this->getServiceLocator()->get('HistoryForm');
$form->setData($this->request->getPost());
if($form->isValid())
{
$model = $this->getServiceLocator()->get('Historymodel');
$arr_sess = $this->getServiceLocator()->get('ImsAcs\Model\AuthStorage')->read();
if($model->insert($form->getData(),$post['hdOfficeId'], $arr_sess['userName']))
{
$this->flashMessenger()->addErrorMessage("Data has been updated Successfully");
}
else
{
$this->flashMessenger()->addErrorMessage("Data was not added");
return $this->redirect()->toRoute('module/default' , array(
'controller'=>'History',
'action' => 'insert',
'id'=>$post['parId']
));
}
}
else
{
print_r($form->errorMessage());
$this->layout()->setVariables(array(
'messages' => array("There were one or more issues with your submission. Please correct them as inticated below."),
)
);
$view = new ViewModel(array('form' => $form, 'action' => $this->getEvent()->getRouteMatch()->getParam('action', 'index')));
$view->setTemplate('model/history/historyForm');
return $view;
}
}
}
在控制器中如果我在$ form-&gt; isValid()之后打印错误信息,我得
数组([effective_till] =&gt;数组([isEmpty] =&gt;数值是必需的, 不能为空))
有人请帮忙解释为什么没有显示自定义验证器消息?