Zend Framework:有没有办法从自定义验证器中访问元素名称?
我正在编写一个自定义验证器,它将验证多个其他表单元素值。 在我的表单中,我这样调用自定义验证器:
$textFieldOne = new Zend_Form_Element_Text('textFieldOne');
$textFieldOne->setAllowEmpty(false)
->addValidator('OnlyOneHasValue', false, array(array('textFieldTwo', 'textFieldThree')));
我的验证器将检查这三个字段(textFieldOne、textFieldTwo、textFieldThree)中只有一个具有值。 我想防止未来的开发人员意外地两次传递同一个字段。
$textFieldOne->addValidator('OnlyOneHasValue', false, array(array('textFieldOne', 'textFieldTwo', 'textFieldThree')));
到目前为止,我的验证器工作得很好,除非我传递与设置了验证器的字段相同的字段名称。
在我的验证器中,您可以看到我正在检查(设置了验证器的元素的值)。 我还检查传递给验证器的其他字段的值。
public function isValid($value, $context = null) {
$this->_setValue($value);
$this->_context = $context;
if ($this->valueIsNotEmpty()) {
if ($this->numberOfFieldsWithAValue() == 0) {
return true;
}
$this->_error(self::MULTIPLE_VALUES);
return false;
}
if ($this->numberOfFieldsWithAValue() == 0) {
$this->_error(self::ALL_EMPTY);
return false;
}
if ($this->numberOfFieldsWithAValue() == 1) {
return true;
}
if ($this->numberOfFieldsWithAValue() > 1) {
$this->_error(self::MULTIPLE_VALUES);
return false;
}
}
private function valueIsNotEmpty() {
return Zend_Validate::is($this->_value, 'NotEmpty');
}
private function numberOfFieldsWithAValue() {
$fieldsWithValue = 0;
foreach ($this->_fieldsToMatch as $fieldName) {
if (isset($this->_context[$fieldName]) && Zend_Validate::is($this->_context[$fieldName], 'NotEmpty')) {
$fieldsWithValue++;
}
}
return $fieldsWithValue;
}
我的解决方案是...
- A. 让开发人员弄清楚有某种方法可以做到这一点。
- B. 忽略
$value
,强制您传递所有元素(这与第一个选项没有太大区别)。 - 或 C.(如果可能)找到首先调用我的验证器的元素的名称,并从
$fieldsWithValue
列表中忽略它。
我认为没有一种方法可以在不将验证器附加到元素的情况下在表单上应用验证器,但如果它是一个选项,那就更好了。
我怎么解决这个问题?
I'm writing a custom validator that will validate against multiple other form element values. In my form, I call my custom validator like this:
$textFieldOne = new Zend_Form_Element_Text('textFieldOne');
$textFieldOne->setAllowEmpty(false)
->addValidator('OnlyOneHasValue', false, array(array('textFieldTwo', 'textFieldThree')));
My validator will check that only one of those three fields (textFieldOne, textFieldTwo, textFieldThree) has a value. I want to prevent a future developer from accidentally passing the same field twice.
$textFieldOne->addValidator('OnlyOneHasValue', false, array(array('textFieldOne', 'textFieldTwo', 'textFieldThree')));
So far, my validator works perfectly, except when I pass the same field name as the field that has the valiator set on it.
In my validator, you can see that I am checking that the value (of the element with the validator set on it). I'm also checking the values of the other fields that were passed to the validator.
public function isValid($value, $context = null) {
$this->_setValue($value);
$this->_context = $context;
if ($this->valueIsNotEmpty()) {
if ($this->numberOfFieldsWithAValue() == 0) {
return true;
}
$this->_error(self::MULTIPLE_VALUES);
return false;
}
if ($this->numberOfFieldsWithAValue() == 0) {
$this->_error(self::ALL_EMPTY);
return false;
}
if ($this->numberOfFieldsWithAValue() == 1) {
return true;
}
if ($this->numberOfFieldsWithAValue() > 1) {
$this->_error(self::MULTIPLE_VALUES);
return false;
}
}
private function valueIsNotEmpty() {
return Zend_Validate::is($this->_value, 'NotEmpty');
}
private function numberOfFieldsWithAValue() {
$fieldsWithValue = 0;
foreach ($this->_fieldsToMatch as $fieldName) {
if (isset($this->_context[$fieldName]) && Zend_Validate::is($this->_context[$fieldName], 'NotEmpty')) {
$fieldsWithValue++;
}
}
return $fieldsWithValue;
}
My solution is to either...
- A. Let the developer figure out there is a certain way to do it.
- B. Ignore
$value
, forcing you to pass all the elements (which isn't much different than the first option). - or C. (if possible) Find the name of the element that called my validator in the first place and ignore it from the list of
$fieldsWithValue
.
I don't think there is a way to apply a validator on a form without attaching it to an element, but that would be even better, if it were an option.
How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常我会建议不要这样做,但是,在这种情况下,我相信类中的静态成员实际上会为这个问题提供一个很好的解决方案。
使用静态成员,您可以在第一次调用 isValid 时将其设置为值,并在后续调用中检查它,从而为您提供了一种机制。
您可能希望将其设置为在配置选项中使用某些数组,以便您可以命名空间并允许验证器的多个实例在不同的集合中愉快地并存。
您真正必须决定如何克服的唯一问题是您希望在哪里显示错误,因为是的,表单本身不接受验证器。 如果您希望第一个之后的所有重复项都显示错误,那么这并不是什么大问题。
Normaly i'd advise against such things, but, in this case I believe a static member in your class would actually provide a good solution to this problem.
With a static member, you can set it to the value in the first time the isValid is called, and check against it in subsequent calls, thus giving you a mechanism for this.
You may want to set this up to use some array in the configuration options, so that you can namespace and allow multiple instances of the validator to exist happily alongside each other for different sets.
The only problem that you really have to decide how to overcome, is where you wish to display the error, as yes the form itself does not take validators. if you want all the duplicates after the first to display an error, it is not so much of a problem.