如何在 Zend_Form::isValid() 方法中获取表单元素值?
我正在寻找在 isValid() 方法中获取表单元素值的最佳方法。
我有类似这样的 isValid():
public function isValid($data) {
$start = (int)($data['start_hour'] . $data['start_minute']);
$end = (int)($data['end_hour'] . $data['end_minute']);
if ($start >= $end) {
$this->getElement('start_hour')->addError('Start time should be less than end time');
return false;
}
return parent::isValid($data);
}
但问题是,当我的数据结构发生变化时,我也必须更改验证。 例如,现在 start_hour、start_month 等的值成为多维数组的元素,我需要像这样的编辑验证
public function isValid($data) {
$start = (int)($data['send']['start_hour'] . $data['send']['start_minute']);
$end = (int)($data['send']['end_hour'] . $data['send']['end_minute']);
.......
}
通过永久键获取元素的值会很棒(像元素名称),所以我的 isValid 可能看起来像:
public function isValid($data) {
$start = (int)($this->getElement('start_hour')->getValue() . $this->getElement('start_minute')->getValue());
$end = (int)($this->getElement('end_hour')->getValue() . $this->getElement('end_minute')->getValue());
.......
}
但是验证方法内的 $this->getElement('start_hour')->getValue() 返回一个空值。
是否可以通过这种方式获取元素值?
谢谢。
I'm looking for best way of getting form element values inside isValid() method.
I had something like this isValid():
public function isValid($data) {
$start = (int)($data['start_hour'] . $data['start_minute']);
$end = (int)($data['end_hour'] . $data['end_minute']);
if ($start >= $end) {
$this->getElement('start_hour')->addError('Start time should be less than end time');
return false;
}
return parent::isValid($data);
}
but problem is that when my data structure changes I have to change validation too.
For example, now values of start_hour, start_minute, etc becomes elements of multidimensional array, and I need edit validation like
public function isValid($data) {
$start = (int)($data['send']['start_hour'] . $data['send']['start_minute']);
$end = (int)($data['send']['end_hour'] . $data['send']['end_minute']);
.......
}
It would be great to get value of element by permanent key (like element name), so my isValid could looks like:
public function isValid($data) {
$start = (int)($this->getElement('start_hour')->getValue() . $this->getElement('start_minute')->getValue());
$end = (int)($this->getElement('end_hour')->getValue() . $this->getElement('end_minute')->getValue());
.......
}
but $this->getElement('start_hour')->getValue() inside validation method return an empty value.
Is this possible to get element value in such way?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 $this->getValue('start_hour');
Try with $this->getValue('start_hour');
如果此代码发生在表单的
isValid()
方法中,那么确保您可以按照您所描述的方式执行此操作。只是isValid()
通常需要传递一些数据 - 例如$form->isValid($_POST)
- 而你最终会忽略它(假设父级是Zend_Form
,它有一个空的isValid()
方法;中间祖先可能会检查传递的数据)。我认为这可能会令人困惑。另一种选择是创建一个 自定义验证器,将其附加到一个表单元素(例如
start_hour
元素)。验证器的isValid()
签名可以使用可选的$context
参数 -isValid($value, $context = null)
。当您调用$form->isValid($data)
时,它会将$data
作为$context
传递给元素验证器。然后,您可以使用该$context
变量来检查其他值(start_min
、end_hour
、end_min
等) 。If this code takes place in the
isValid()
method of the form, then sure you could do it as you have described. It's just thatisValid()
usually needs some data passed -$form->isValid($_POST)
, for example - and you just end up ignoring it (assuming the parent isZend_Form
, which has an emptyisValid()
method; intermediate ancestors could potentially inspect the passed data). I would consider that to be potentially confusing.Al alternative could be to create a custom validator, attach it to one of the form elements (say, the
start_hour
elements). The signature for the validator'sisValid()
can use the optional$context
parameter -isValid($value, $context = null)
. When you call$form->isValid($data)
, it will pass that$data
as the$context
to the the element validator. You can then use that$context
variable to inspect the other values (start_min
,end_hour
,end_min
, etc).尝试
在调用 isValid 之前调用,这样数据就会出现在您的表单中。
然后
$this->getValue('start_hour');
应该在isValid()
中工作。所以可以肯定的是:
在你的代码中的某个地方(可能是控制器)有类似的东西:
Try calling
Before calling isValid that way the data will be in your form.
Then
$this->getValue('start_hour');
should work from withinisValid()
.So to be sure:
Somewhere in your code (probably controller) there is somthing like: