Search code examples
phpzend-frameworkzend-validatezend-filter

Zend Form Validation and Filtering without Zend Forms


Is there a way to validate and filter pure HTML forms with Zend Validation and Zend Filter classes ? Please give some example if you have, I couldn't find out any.


Solution

  • $email           = $this->_getParam('email');// form's $_POST or $_GET
    $email_validator = new Zend_Validate_EmailAddress();
    
    if(!$email_validator->isValid($email)){
        // Error, throw(Exception)
    }
    

    To see what validation you can use, open your library folder and go to library/Zend/Validate/ path, you will see many files/classes. That are available classes for validation like Alnum.php, Alpha.php, Barcode.php, CreditCard.php, etc. Just create instance and call method isValid();

    new Zend_Validate_FileName();
    

    The same thing is for filters, on path library/Zend/Filter/ you can see a lot of classes. Almost all of them implement an interface Zend_Filter_Interface(Interface.php) where you can find one method:

    public function filter($value);