如何将参数传递给自定义 Zend 验证器?

发布于 2024-10-18 23:54:01 字数 755 浏览 3 评论 0原文

我正在尝试在 Zend 框架中编写一个验证器。

验证器查询数据库以检查特定记录是否存在,此查询使用 where 子句。 where 子句的值由表单上的另一个字段指定,那么如何将该值传递到验证器中呢?

这就是我添加验证器的方式:

$adgroup_name->addValidator(new Generic_ValidateUniqueAdGroupName() ); break;

在我的验证器中,我有:

// Query which gets an array of existing ad group names in db
$q = Doctrine_Query::create()
->select('a.name')
->from('AdGroup a')
->where('a.name = ?', $value)
->andWhere('a.campaign_id = ?', $campaign_id);      
$adgroup_names_result = $q->fetchOne(array(), Doctrine::HYDRATE_ARRAY);     

如何传入 $campaign_id?我已尝试以下方法但不起作用:

$adgroup_name->addValidator(new Generic_ValidateUniqueAdGroupName($campaign_id) ); break;

I am trying to write a validator in Zend framework.

The validator queries the database to check if a particular record exists, this query uses a where clause. The value for the where clause is specified by another field on the form, so how do I pass this value into the validator?

This is how I add my validator:

$adgroup_name->addValidator(new Generic_ValidateUniqueAdGroupName() ); break;

Within my validator I have:

// Query which gets an array of existing ad group names in db
$q = Doctrine_Query::create()
->select('a.name')
->from('AdGroup a')
->where('a.name = ?', $value)
->andWhere('a.campaign_id = ?', $campaign_id);      
$adgroup_names_result = $q->fetchOne(array(), Doctrine::HYDRATE_ARRAY);     

How do I pass in $campaign_id? I've tried the following and it doesn't work:

$adgroup_name->addValidator(new Generic_ValidateUniqueAdGroupName($campaign_id) ); break;

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

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

发布评论

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

评论(3

蓝海似她心 2024-10-25 23:54:01

原理与“确认密码”验证器中使用的原理相同。提交表单时,您需要在字段中获取表单中另一个元素的值。。这就是尝试使用 Campaign_id 实例化验证器不起作用的原因:$campaign_id 的值尚未设置。

当元素在其验证器上调用 isValid() 时,它会传递名为 $context 的第二个参数,该参数填充了提交的表单值的数组。因此,验证器中的 isValid() 方法应如下所示:

public function isValid($value, $context = null)
{
    $campaign_id = $context['campaign_id'];
    // Now build your query using $value - from the element to which this 
    // validator is attached - and the $campaign_id provided by the context.
    // And, of course, return true/false as required.
}

The principle is the same as used in "confirm password" validators. You need the value of another element in the form in the field when the form is submitted. That's why attempting to instantiate the validator with the campaign_id is not working: the value of $campaign_id is not yet set.

When an element calls isValid() on its validators, it passes a second parameter called $context that is filled with an array of submitted form values. So your isValid() method in your validator should look something like this:

public function isValid($value, $context = null)
{
    $campaign_id = $context['campaign_id'];
    // Now build your query using $value - from the element to which this 
    // validator is attached - and the $campaign_id provided by the context.
    // And, of course, return true/false as required.
}
跨年 2024-10-25 23:54:01

我认为要使 new Generic_ValidateUniqueAdGroupName($campaign_id) 工作,您需要为 Generic_ValidateUniqueAdGroupName 类定义一个构造函数和一个名为 $_campaign_id 的变量。草稿如下:

class  Generic_ValidateUniqueAdGroupName extends Zend_Validate_Abstract {

   protected $_campaign_id;

   public function __construct($campaign_id) {
      $this->_campaign_id = $campaign_id;
   }

}

通过此,您可以在查询中以 $this->_campaign_id 的形式访问 id。

I think to make new Generic_ValidateUniqueAdGroupName($campaign_id) work you need to define a constructor for Generic_ValidateUniqueAdGroupName class and a variable called e.g. $_campaign_id. A draft is below:

class  Generic_ValidateUniqueAdGroupName extends Zend_Validate_Abstract {

   protected $_campaign_id;

   public function __construct($campaign_id) {
      $this->_campaign_id = $campaign_id;
   }

}

With this, you would access the id in your query as $this->_campaign_id.

半暖夏伤 2024-10-25 23:54:01

您需要提供 $options 参数:

Zend_Form_Element:

public function addValidator($validator, $breakChainOnFailure = false, $options = array())

所以,你会这样做:

$validator = new new Generic_ValidateUniqueAdGroupName();
$adgroup_name
    ->addValidator($validator, false, array('campaign_id' => 1));

You need to supply the $options argument:

Implementation in Zend_Form_Element:

public function addValidator($validator, $breakChainOnFailure = false, $options = array())

So, you'd do something like this:

$validator = new new Generic_ValidateUniqueAdGroupName();
$adgroup_name
    ->addValidator($validator, false, array('campaign_id' => 1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文