VDaemon PHP Library | VDaemon Extension | Table of Contents

VDaemon Tutorial
Defining Validation Rules

It is a time to add validation rules to the form. All validation is defined by the vlvalidator and vlgroup VDaemon custom tags. These tags have many attributes which allow defining all of the common validation types as well as creating custom validation logic. To learn all of the validator features see the reference.

In this tutorial we will validate "Name" input field. We will check that visitor enters some value to it and this value is valid name (we assume that name can contain only letters, spaces and apostrophe character).

We will use "required" validator to check that the "Name" is filled with some value. It is most common validator type. Let's examine the code of the "required" validator:
<vlvalidator name="NameReq" type="required" control="Name" errmsg="Name required">
The validator attributes shown above are common to all of the types of validators. Each validator must have a unique name (name="NameReq"). The validation type is defined by "type" attribute (type="required"). The "control" attribute specifies the input element to validate (control="Name"). Lastly, the "errmsg" attribute contains error message that will be displayed on the form page if validation test fails for this vaildator.

To check for valid name we will use "regexp" validator. It validates entered value against regular expression pattern. In our case this pattern is "/^[a-z'\s]*$/i":
<vlvalidator name="NameRegExp" type="regexp" control="Name" errmsg="Invalid Name" regexp="/^[a-z'\s]*$/i">

After adding validation elements your page source code should look as shown below.

<?php include('vdaemon/vdaemon.php'); ?>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<p>Hello form.</p>
<form method="POST" action="hello.php" id="Hello" runat="vdaemon">
  <table cellpadding="2" cellspacing="0" border="0">
    <tr>
      <td>
        Enter your Name:
      </td>
      <td>
        <input name="Name" type="text" size="25">
        <vlvalidator name="NameReq" type="required" control="Name" errmsg="Name required">
        <vlvalidator name="NameRegExp" type="regexp" control="Name"
          errmsg="Invalid Name" regexp="/^[a-z'\s]*$/i">
      </td>
    <tr>
      <td colspan="2">
        <input type="submit" value="Send">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<?php VDEnd(); ?>

Now if you run the form you will see that you can't submit empty string or value with digits or special characters. Validation works, but we don't see any error messages yet.

Continue to Displaying Error Messages