Skip navigation.

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:

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.



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 may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Use the special tag [adsense:format:group:channel] or [adsense:block:location] to display Google AdSense ads.

More information about formatting options

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