Validate URI In Form Fields With Zend Framework Custom Validator

Introduction to Zend Framework

Zend Framework is an open source, object-oriented web application framework implemented in PHP 5 and licensed under the New BSD License. Zend Framework provides many components like Zend_Form, Zend_Controller, Zend_Validate, Zend_Filter to build web secure and robust applications fairly faster.

To know more about Zend Framework visit the following websites:

  • http://framework.zend.com/
  • http://en.wikipedia.org/wiki/Zend_Framework

Zend_Form:

Zend_Form official reference guide is available at http://framework.zend.com/manual/en/zend.form.html

Zend_Form simplifies form creation and handling in your web application. Taking advantage of Zend_Filter and Zend_Validate you can filter and validate your form fields. At the time of writing this article 19 validation classes are shipped with Zend Framework. I came across a situation where I had to write a custom class to validate URIs. To write the custom URI validator I made use of Zend_Uri which aids in manipulating and validating Uniform Resource Identifiers (URIs). In particular I used Zend_Uri::check() function to validate the URI in question.

BV_Validate_Uri - Custom Validator To Check Whether A Given URI Is Valid

In order to use BV_Validate_Uri you must have set up your application to use Zend Framework. You must also know how to use Zend_Form to validate the form fields.

Create the directory BV under your Zend Framework library directory. Also create the directory Validate under BV. Create a file by name Uri.php and copy the PHP code to it.

mkdir library/BV
mkdir library/BV/Validate
vi library/BV/Validate/Uri.php
Copy the below PHP code and save the file


<?php
require_once 'Zend/Validate/Abstract.php';
require_once 
'Zend/Uri.php';


class 
BV_Validate_Uri extends Zend_Validate_Abstract
{
    const 
MSG_URI 'msgUri';

    protected 
$_messageTemplates = array(
        
self::MSG_URI => "Invalid URI",
    );

    public function 
isValid($value)
    {
        
$this->_setValue($value);

        
//Validate the URI
        
$valid Zend_Uri::check($value);
        
        
//Return validation result TRUE|FALSE   
        
if ($valid)  {
            return 
true;
        } else {
            
$this->_error(self::MSG_URI);
            return 
false;
        
        }

    }
}

?>

Now we have created a reusable URI validator. You can use this URI validator in all your forms. Below is an example of the BV_Validate_Uri usage with Zend_Form.


<?php

 $website 
$form->createElement('text''website');
            
$website->setLabel('Website URL');
            
$website->addValidator(new BV_Validate_Uri());
?>

That is all you need to do to add the BV_Validate_Uri to your form field. When the form is submitted the element is validated by BV_Validate_Uri. In summary, you just have to add one line to add the custom URI validator.


<?php
            $website
->addValidator(new BV_Validate_Uri());
?>

I hope this article has helped you to validate URI in your form fields.

About the author

Sudheer is an entrepreneur and software developer. Get more from Sudheer on Twitter.


Nice validator!

Nice validator!

Custom validation messages for Captcha component

Hi,
I've been working with Zend framework a bit and have a little information for those who need custom validation messages for captcha (Zend_Form_Element_Captcha) component. It's a bit frustrating so I paste code there (hopefully everyone who needs it will find it):

$form->captcha = new Zend_Form_Element_Captcha('captcha', array(
                    label => 'Prove that you are human',
                    captcha => "Figlet",
                    captchaOptions => array (
                        'captcha' => "Figlet",
                        'wordLen' => 6,
                        'timeout' => 300,
                        'messages' => array (
                        Zend_Captcha_Word::BAD_CAPTCHA => 'Wrong word or symbol combination entered',
                        Zend_Captcha_Word::MISSING_VALUE => "No word is entered",
                        Zend_Captcha_Word::MISSING_ID => "Field ID is missing or smth like that"
                    )),    
                ));

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.