如何在 Zend_Form::isValid() 方法中获取表单元素值?

发布于 2024-12-25 05:23:52 字数 1307 浏览 1 评论 0原文

我正在寻找在 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_hourstart_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪 2025-01-01 05:23:52

尝试使用 $this->getValue('start_hour');

Try with $this->getValue('start_hour');

月朦胧 2025-01-01 05:23:52

如果此代码发生在表单的 isValid() 方法中,那么确保您可以按照您所描述的方式执行此操作。只是 isValid() 通常需要传递一些数据 - 例如 $form->isValid($_POST) - 而你最终会忽略它(假设父级是 Zend_Form,它有一个空的 isValid() 方法;中间祖先可能会检查传递的数据)。我认为这可能会令人困惑。

另一种选择是创建一个 自定义验证器,将其附加到一个表单元素(例如start_hour元素)。验证器的 isValid() 签名可以使用可选的 $context 参数 - isValid($value, $context = null)。当您调用 $form->isValid($data) 时,它会将 $data 作为 $context 传递给元素验证器。然后,您可以使用该 $context 变量来检查其他值(start_minend_hourend_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 that isValid() usually needs some data passed - $form->isValid($_POST), for example - and you just end up ignoring it (assuming the parent is Zend_Form, which has an empty isValid() 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's isValid() 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).

孤独岁月 2025-01-01 05:23:52

尝试

$form->populate($data)

在调用 isValid 之前调用,这样数据就会出现在您的表单中。
然后 $this->getValue('start_hour'); 应该在 isValid() 中工作。

所以可以肯定的是:
在你的代码中的某个地方(可能是控制器)有类似的东西:

    $this->view->form = new MyForm();
$this->populate($data); //add this
if($this->view->form->isValid($data)){
//do stuff
}

Try calling

$form->populate($data)

Before calling isValid that way the data will be in your form.
Then $this->getValue('start_hour'); should work from within isValid().

So to be sure:
Somewhere in your code (probably controller) there is somthing like:

    $this->view->form = new MyForm();
$this->populate($data); //add this
if($this->view->form->isValid($data)){
//do stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文