Php 是否根据元素/字段值以Zend_形式进行验证?
让我们想象一下,我们有这样一个问题:“你是先生/夫人吗?” 根据答案值,我们将实施进一步的验证 比如说,, Mr>验证喜爱的车型 夫人:我最喜欢的花 是否可以覆盖Php 是否根据元素/字段值以Zend_形式进行验证?,php,zend-framework,zend-form,zend-validate,Php,Zend Framework,Zend Form,Zend Validate,让我们想象一下,我们有这样一个问题:“你是先生/夫人吗?” 根据答案值,我们将实施进一步的验证 比如说,, Mr>验证喜爱的车型 夫人:我最喜欢的花 是否可以覆盖isValid功能? 也许有一些最佳实践的例子?我会编写一个自定义验证器,并使用提供的$context变量 一个简短的例子 控制器 class MyController extends Zend_Controller_Action { public function indexAction() { $form =
isValid
功能?
也许有一些最佳实践的例子?我会编写一个自定义验证器,并使用提供的
$context
变量
一个简短的例子
控制器
class MyController extends Zend_Controller_Action {
public function indexAction() {
$form = new Application_Form_Gender();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
/*...*/
}
}
}
}
class MyValidator extends Zend_Validate_Abstract {
const ERROR = 'error';
protected $_gender;
protected $_messageTemplates = array(
self::ERROR => "Your gender is %gender%, so you have to enter something here",
);
protected $_messageVariables = array('gender' => '_gender');
function __construct($gender) {
$this->_gender = $gender;
}
function isValid( $value, $context = null ) {
if (!isset($context['radio1'])) {
return true;
}
if ($context['radio1'] != $this->_gender) {
return true;
}
if (empty($context[sprintf('text%s', $this->_gender)])) {
$this->_error(self::ERROR);
return false;
}
return true;
}
}
表格
class Application_Form_Gender extends Zend_Form {
public function init()
{
$this->addElement('radio', 'radio1', array('multiOptions' => array('m' => 'male', 'w' => 'female')));
$this->getElement('radio1')->isRequired(true);
$this->getElement('radio1')->setAllowEmpty(false);
$this->addElement('text', 'textm', array('label' => 'If you are male enter something here');
$this->getElement('textm')->setAllowEmpty(false)->addValidator(new MyValidator('m'));
$this->addElement('text', 'textf', array('label' => 'If you are female enter something here'));
$this->getElement('textf')->setAllowEmpty(false)->addValidator(new MyValidator('f'));
$this->addElement('submit', 'submit');
}
验证程序
class MyController extends Zend_Controller_Action {
public function indexAction() {
$form = new Application_Form_Gender();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
/*...*/
}
}
}
}
class MyValidator extends Zend_Validate_Abstract {
const ERROR = 'error';
protected $_gender;
protected $_messageTemplates = array(
self::ERROR => "Your gender is %gender%, so you have to enter something here",
);
protected $_messageVariables = array('gender' => '_gender');
function __construct($gender) {
$this->_gender = $gender;
}
function isValid( $value, $context = null ) {
if (!isset($context['radio1'])) {
return true;
}
if ($context['radio1'] != $this->_gender) {
return true;
}
if (empty($context[sprintf('text%s', $this->_gender)])) {
$this->_error(self::ERROR);
return false;
}
return true;
}
}
正如您在本例中所看到的,在
$form->isValid()
中提供的所有数据都是通过$context
变量提供的,您可以使用该变量执行任何检查。我将编写一个自定义验证器,并使用提供的$context
变量
一个简短的例子
控制器
class MyController extends Zend_Controller_Action {
public function indexAction() {
$form = new Application_Form_Gender();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
/*...*/
}
}
}
}
class MyValidator extends Zend_Validate_Abstract {
const ERROR = 'error';
protected $_gender;
protected $_messageTemplates = array(
self::ERROR => "Your gender is %gender%, so you have to enter something here",
);
protected $_messageVariables = array('gender' => '_gender');
function __construct($gender) {
$this->_gender = $gender;
}
function isValid( $value, $context = null ) {
if (!isset($context['radio1'])) {
return true;
}
if ($context['radio1'] != $this->_gender) {
return true;
}
if (empty($context[sprintf('text%s', $this->_gender)])) {
$this->_error(self::ERROR);
return false;
}
return true;
}
}
表格
class Application_Form_Gender extends Zend_Form {
public function init()
{
$this->addElement('radio', 'radio1', array('multiOptions' => array('m' => 'male', 'w' => 'female')));
$this->getElement('radio1')->isRequired(true);
$this->getElement('radio1')->setAllowEmpty(false);
$this->addElement('text', 'textm', array('label' => 'If you are male enter something here');
$this->getElement('textm')->setAllowEmpty(false)->addValidator(new MyValidator('m'));
$this->addElement('text', 'textf', array('label' => 'If you are female enter something here'));
$this->getElement('textf')->setAllowEmpty(false)->addValidator(new MyValidator('f'));
$this->addElement('submit', 'submit');
}
验证程序
class MyController extends Zend_Controller_Action {
public function indexAction() {
$form = new Application_Form_Gender();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
/*...*/
}
}
}
}
class MyValidator extends Zend_Validate_Abstract {
const ERROR = 'error';
protected $_gender;
protected $_messageTemplates = array(
self::ERROR => "Your gender is %gender%, so you have to enter something here",
);
protected $_messageVariables = array('gender' => '_gender');
function __construct($gender) {
$this->_gender = $gender;
}
function isValid( $value, $context = null ) {
if (!isset($context['radio1'])) {
return true;
}
if ($context['radio1'] != $this->_gender) {
return true;
}
if (empty($context[sprintf('text%s', $this->_gender)])) {
$this->_error(self::ERROR);
return false;
}
return true;
}
}
正如您在本例中所看到的,在
$form->isValid()
中提供的所有数据都是通过$context
变量提供的,您可以使用该变量执行任何检查。我发现重写isValid
对我来说最方便。您还可以尝试使用$context
第二个参数的自定义验证器。但是它有点麻烦,因为您无法直接访问表单,这有时可能很方便/需要。我发现重写isValid
对我来说最方便。您还可以尝试使用$context
第二个参数的自定义验证器。但是它有点麻烦,因为您没有直接访问表单的权限,这有时可能很方便/需要。