Zend Framework 表单元素验证器 - 即使不需要也验证字段

发布于 2024-09-04 22:58:35 字数 1095 浏览 7 评论 0原文

即使不需要表单元素,有没有办法让验证器触发?

我有一个表单,如果另一个表单元素(几个单选按钮)的值选择了特定值,我想验证文本框的内容(确保不为空)。现在我正在通过重写表单类的 isValid() 函数来做到这一点,并且效果很好。但是,我想将其移至其验证器或使用回调验证器。这是我到目前为止所拥有的,但它似乎永远不会被调用,除非我将字段更改为 setRequired(true) ,而我不想一直这样做,只有当其他表单元素的值设置为特定时价值。

// In my form class's init function
$budget = new Zend_Form_Element_Radio('budget');
$budget->setLabel('Budget')
    ->setRequired(true)
    ->setMultiOptions($options);

$budgetAmount = new Zend_Form_Element_Text('budget_amount');
$budgetAmount->setLabel('Budget Amount')
 ->setRequired(false)
 ->addFilter('StringTrim')
 ->addValidator(new App_Validate_BudgetAmount());

//Here is my custom validator (incomplete) but just testing to see if it even gets called.
class App_Validate_BudgetAmount extends Zend_Validate_Abstract
{
    const STRING_EMPTY = 'stringEmpty';

    protected $_messageTemplates = array(
        self::STRING_EMPTY => 'please provide a budget amount'
    );

    public function isValid($value)
    {
        echo 'validating...';
        var_dump($value);
        return true;
    }
}

Is there a way to get a validator to fire even if the form element isn't required?

I have a form where I want to validate the contents of a texbox (make sure not empty) if the value of another form element, which is a couple of radio buttons, has a specific value selected. Right now I'm doing this by overriding the isValid() function of my form class and it works great. However, I'd like to move this to either its on validator or use the Callback validator. Here's what I have so far, but it never seems to get called unless I change the field to setRequired(true) which I don't want to do at all times, only if the value of the other form element is set to a specific value.

// In my form class's init function
$budget = new Zend_Form_Element_Radio('budget');
$budget->setLabel('Budget')
    ->setRequired(true)
    ->setMultiOptions($options);

$budgetAmount = new Zend_Form_Element_Text('budget_amount');
$budgetAmount->setLabel('Budget Amount')
 ->setRequired(false)
 ->addFilter('StringTrim')
 ->addValidator(new App_Validate_BudgetAmount());

//Here is my custom validator (incomplete) but just testing to see if it even gets called.
class App_Validate_BudgetAmount extends Zend_Validate_Abstract
{
    const STRING_EMPTY = 'stringEmpty';

    protected $_messageTemplates = array(
        self::STRING_EMPTY => 'please provide a budget amount'
    );

    public function isValid($value)
    {
        echo 'validating...';
        var_dump($value);
        return true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

落日海湾 2024-09-11 22:58:35

看起来如果您使用 setAllowEmpty(false) 代替 setRequired(false) 那么您的验证器仍然会被调用。所以我现在有了:

$budgetAmount->setLabel('Budget Amount')
    ->setAllowEmpty(false)
    ->addFilter('StringTrim')
    ->addValidator(new App_Validate_BudgetAmount());

而且效果很好

Looks like if you use setAllowEmpty(false) in place of setRequired(false) then you validator will still get called. So I now have:

$budgetAmount->setLabel('Budget Amount')
    ->setAllowEmpty(false)
    ->addFilter('StringTrim')
    ->addValidator(new App_Validate_BudgetAmount());

and it is working great

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文