How to use validators

crdate : 2008/03/12

by : Luc Muller

 in : Tutorial

This tutorial will show you how to use some of the validators.

 

we will demonstrate those validators

 

validator:STANDARD : required, email, too short, too long, right size, sameas, authentified.

validator:NUM : is numeric, is lower than, is higher than, is between, is int, is float

Validator:FILE : extension, filzesize, filesizekb, filesizemb.

validator:DB : unique

To use validators. you'll need to create a simple formidable with a few renderlet.

let's start with a renderlet:TEXT

<renderlet:TEXT name="mytxt" label="My text field" />

Standard Validators

1. Make a field required

add those line to your renderlet:TEXT

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:STANDARD>
      <required message="text field is required" />
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

Now your field is required, when you click on submit the message specified in "message" appears at the top of your form (if in renderer:STANDARD)

2. Validate that a field is an email

add those line to your renderlet:

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:STANDARD>
      <email>
        <message>This is not an email</message>
      </email>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

From this point, your form won't validate until your field is field with a valid email address.

3. Validate that the field is not too short

add those line to your renderlet

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:STANDARD>
      <minsize>
        <value>10</value>
        <message>Your text is to short</message>
      </minsize>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

You field will not validate until it contains at least 10 characters

this can be useful for check password for instance

4. Validate that the field is not long

It's the same as above, but with maxsize

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:STANDARD>
      <maxsize>
        <value>10</value>
        <message>Your text is to long</message>
      </maxsize>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

Your field will not validate until it contains less than 10 characters.

5. Validate that the field has the right length

If you want to check if your renderlet has the desired length add those lines

<renderlet:TEXT name="mytxt" label="My text field">
   <validators>
     <validator:STANDARD>
       <size>
         <value>10</value>
         <message>Your text has not the right size</message>
      </size>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

This will check that the field has strictly the length that you want

6. Validate that the field is the same has the same value as another

for this purpose add another renderlet:TEXT to your form and add a validator : sameas

<renderlet:TEXT name="mytxt" label="My text field" />

<renderlet:TEXT name="mysecondtxt" label="This text field should be equal to the other">
  <validators>
    <validator:STANDARD>
      <sameas>
        <value>mytxt</value>
        <message>The second text field is not the same as the first one</message>
      </sameas>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

Your form won't validate until the fields are strictly the same.

Can also be usefull to check the validity of a password for instance.

7. Validate the the user is authentified

adding those line will check that the user has to be logged in to submit the field

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:STANDARD>
      <authentified>
        <value>TRUE</value>
        <message>You must be looged in to complete this form</message>
      </authentified>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

Now Formidable will check if the user is logged in. in another way you can check if the user is not logged in with value=>false

Numeric Validators

1. Validate that the field is numeric

You can check that a field is filled with a numeric value for this add those lines

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:NUM>
      <isnum>
        <message>Your field should be numeric</message>
      </isnum>
    </validator:NUM>
  </validators>
</renderlet:TEXT>

This will check that the value entered is numeric but do care!!

this is numeric : 1.2

but this is not : 1,2

2. Validate that the field is lower than a number

to check if a renderlet is lower than a number add those lines

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:NUM>
      <islower>
        <value>100</value>
        <message>Your field should be lower than 100</message>
      </islower>
    </validator:NUM>
  </validators>
</renderlet:TEXT>

Hint to check if a field is lower or equal to a number add +1 to the value

3. Validate that the field is higher than a number

to check if the field is higher than a number add those lines

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:NUM>
      <ishigher>
        <value>100</value>
        <message>Your field should be higher than 100</message>
      </ishigher>
    </validator:NUM>
  </validators>
</renderlet:TEXT>

Hint : to check if a field is higher or equal to a number add -1 to the value

4. Validate that the field is between to numbers

add those lines to check if the value entered is between two numbers

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:NUM>
      <isbetween>
        <value>50,100</value>
        <message>Your field should be between 50 and 100</message>
      </isbetween>
    </validator:NUM>
  </validators>
</renderlet:TEXT>

Do care!! the limits are considered as correct values if you want the value to be stricly between the limits add +1 to the lower value and -1 to the higher

5. Validate that the value is an integer

to validate that the value is an integer add those lines

 <renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:NUM>
      <isinteger>
        <message>Your field should be an integer</message>
      </isinteger>
    </validator:NUM>
  </validators>
</renderlet:TEXT>

This will check if your value is an integer.

6. Validate that the value is a float

This will validate your value as a float

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:NUM>
      <isfloat>
        <message>Your field should be a float</message>
      </isfloat>
    </validator:NUM>
  </validators>
</renderlet:TEXT>

This will validate that your value is strictly a float. it means that :

this will validate : 100.50

but this will not : 100 <= this is an integer

Validator File

first : add a <renderlet:UPLOAD /> to your form

1. Validate the file extension

to validate the file type use this validator on a file add this validator to your renderlet file

<renderlet:UPLOAD name="myfile" label="My file field">
  <data targetDir="uploads" />
  <validators>
    <validator:FILE>
      <extension>
        <value>jpg,gif,png</value>
        <message>Your file should be either a jpg, gif or png</message>
      </extension>
    </validator:FILE>
  </validators>
</renderlet:UPLOAD>

This is going to check the extension of the file and will show the error message until your file has not one of the extension mentionned in the value

2. Validate the filesize

to validate the size of your file you can use 3 validators. filesize, filesizekb, filesizemb

use one of them like this :

<renderlet:UPLOAD name="myfile" label="My file field">
 <data targetDir="uploads" />
   <validators>
     <validator:FILE>
       <filesizekb>
         <value>10</value>
         <message>Your file is to big</message>
       </filesizekb>
     </validator:FILE>
  </validators>
</renderlet:UPLOAD>

This will check that the size of your file is not over 10kb.

Hint : use a size validator in accordance with the size of the file you're expecting to be post. e.g. do not use filesize if you expect that users are going ton send avi movies.

Validator DB

Those validators are made to validate your fileds against database values.

1. Validate that the value of the field is unique in the database

to validate that the value you are inpputting is not already present in the database use this validator

<renderlet:TEXT name="mytxt" label="My text field">
  <validators>
    <validator:DB>
      <unique>
        <tablename>fe_users</tablename>
        <field>username</field>
        <message>This username does already exist in our database</message>
         <deleted>true</deleted>
      </unique>
    </validator:DB>
  </validators>
</renderlet:TEXT>

This will check if the value is not present in the field for the tablename. deleted means that it will check with or not the deleted items if deleted is set to true then items deleted will not be checked

More

Of course you can combine together as much validator as you need.

and even more...

you can use a userobj validator

Validate with userobj

Use this standard validator to validate with a userobj.

<renderlet:TEXT name="userobj" label="My text field is validate by a userobj">
    <validators>
      <validator:STANDARD>
        <userobj message="this word is not allowed here">
          <php><![CDATA[

            $aData = $this->getUserObjParams();

            if(!empty($aData["value"])) {

              if(strstr(strtolower($aData["value"]), "microsoft")) {

                return FALSE;

              }

            }
          return TRUE;
        ]]></php>
      </userobj>
    </validator:STANDARD>
  </validators>
</renderlet:TEXT>

You can download the XML with all those validator in the related files or see it live there.

« Back to list