2

How should Zend_Validator_StringLength extended?
MyValidator:

class Zend_Validate_StringLengthNoTags extends Zend_Validate_StringLength {

    public function  __construct($options = array()) {
        parent::__construct($options);
    }

    public function isValid($value) {
        return parent::isValid(trim(strip_tags($value)));
    }
}

Use of validator - bug not assign values to $this->_min, $this->_max:

$text->addValidator(new Zend_Validator_StringLengthNoTags(array('max'=>4,'min'=>2)));

Edit:

root of bug: $this->_min==1, $this->_max==null , but it should be

$this->_min==2, $this->_max==4

Update: the answer: This was internal application system problem that generate this bug, it fixed soo the code above working. Thanks for all peaple they try to help me.

Thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
Ben
  • 25,389
  • 34
  • 109
  • 165

1 Answers1

1

It seems that your Zend_Validate_StringLengthNoTags could be replaced simply by filters StripTags and StringTrim and standard Zend_Validate_StringLength validator.

For example, I think that the following would work the same as your validator:

   $text->setFilters(array('stripTags','stringTrim'));
   $text->addValidator(new Zend_Validator_StringLength(array('max'=>4,'min'=>2)));

The reason is that validation in Zend Framework is performed after filtering, i.e. on filtered data.

Returning to your Zend_Validate_StringLengthNoTags code. The code looks ok and I cannot spote a problem with it.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • thanks, but It can not replaced by filter, I wanr to remove html tags for validation and not for something else. The input should be stored in data base with html tags – Ben Jan 31 '11 at 12:39
  • I, too, don't see a problem with the validator as written. Of course, the difference between adding `StripTags` as a filter and using his validator is in the value he gets when he ultimately calls `$form->getValues()`. If added as a filter, the the tags will be stripped. If the filtering is done only in the validator, then the tags will still be there. – David Weinraub Jan 31 '11 at 12:41
  • @Yosef. Ok, now I understand. – Marcin Jan 31 '11 at 12:42
  • @David Weinraub. Yes, now I see what Yosef is trying to do. But still I cannot see whats wrong with code of Zend_Validate_StringLengthNoTags. I even copied this validator to my own project and it works as it supposed. – Marcin Jan 31 '11 at 13:02
  • Indeed. Sure looks like it ought to work. Anyway, looks like he found the answer and you got an accept out of it. Win, win! ;-) – David Weinraub Jan 31 '11 at 16:43