cvs: peardoc /en/package/html/html-quickform intro-validation.xml
| From: | Alexey Borzov | Date: | Wed, 28 May 2003 15:30:33 +0000 |
| Subject: | cvs: peardoc /en/package/html/html-quickform intro-validation.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-854@lists.php.net to get a copy of this message | ||
avb Wed May 28 11:30:33 2003 EDT
Modified files:
/peardoc/en/package/html/html-quickform intro-validation.xml
Log:
Converted Bertrand's validation docs, removed redundant package maintainer info from
Introduction
Index: peardoc/en/package/html/html-quickform/intro-validation.xml diff -u peardoc/en/package/html/html-quickform/intro-validation.xml:1.1 peardoc/en/package/html/html-quickform/intro-validation.xml:1.2 --- peardoc/en/package/html/html-quickform/intro-validation.xml:1.1 Fri May 23 10:21:32 2003 +++ peardoc/en/package/html/html-quickform/intro-validation.xml Wed May 28 11:30:33 2003 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <refentry id="package.html.html-quickform.intro-validation"> <refnamediv> <refname>Introduction - validation and filters</refname> @@ -7,9 +7,212 @@ <refsect1 id="package.html.html-quickform.intro-validation.rules"> <title>Validation rules</title> <para> - To be converted +QuickForm makes client-side and server-side form validation easy. It allows for validation against regular +expressions or custom functions and methods. You can define your own validation rules and apply them to +the elements or groups you want. In this section, we will explore the different possibilities QuickForm +offers to make validation easier. + </para> + <para> +QuickForm can verify if required elements are filled when the form is submitted. This works with every type of elements or groups. QuickForm uses an internal regular expression to check if the element is empty. The 0 integer is not considered as an empty value. The fact that an element is required triggers the use of the other validation rules set for that same element. +<programlisting role="php"> +<![CDATA[ +require_once 'HTML/QuickForm.php'; + +$form = new HTML_QuickForm('myform', 'post'); +$form->addElement('text', 'email', 'Your email:'); +$form->addElement('submit', 'submit', 'Submit'); + +// Validation rules + +$form->addRule('email', 'E-Mail is required', 'required'); + +// Validation + +if ($form->validate()) { + $form->freeze(); +} +$form->display(); +]]> +</programlisting> + </para> + <para> +The <function>HTML_QuickForm::validate</function> method will scan through each rules in the order they have been set. If a rule is not validated, the error message corresponding to the element will be displayed and the form will be shown once again. You can use templates to set the position of this error message. The order you decide to set your validation rules is important because it will determine which error message is used. </para> + <refsect2 id="package.html.html-quickform.intro-validation.rules.client"> + <title>Client-side validation</title> + <para> +QuickForm can generate the javascript necessary to validate the form on the client side. This feature works for every standard elements. Groups will be validated on the client side by concatenating all grouped elements value into a single value. Server side validation is always performed in case the client has javascript turned off. +<programlisting role="php"> +<![CDATA[ +$form->addRule('email', 'E-Mail is required', 'required', null, 'client'); +]]> +</programlisting> + </para> + <tip><para> +By setting the parameter 'client', you trigger the javascript automatic generation. + </para></tip> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.builtin"> + <title>Built-in validation rules</title> + <para> +QuickForm offers a few registered rules that are often used when validating forms. These actually are regular expressions ready to use, 'required' is one of those. + <variablelist> + <varlistentry> + <term><varname>required</varname></term> + <listitem><para>value is not empty</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>maxlength</varname></term> + <listitem><para>value must not exceed <parameter>n</parameter> characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>minlength</varname></term> + <listitem><para>value must have more than <parameter>n</parameter> characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>rangelength</varname></term> + <listitem><para>value must have between <parameter>m</parameter> and <parameter>n</parameter> characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>regex</varname></term> + <listitem><para>value must pass the regular expression</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>email</varname></term> + <listitem><para>value is a correctly formatted email</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>lettersonly</varname></term> + <listitem><para>value must contain only letters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>alphanumeric</varname></term> + <listitem><para>value must contain only letters and numbers</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>numeric</varname></term> + <listitem><para>value must be a number</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>nopunctuation</varname></term> + <listitem><para>value must not contain punctuation characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>nonzero</varname></term> + <listitem><para>value must be a number not starting with 0</para></listitem> + </varlistentry> + </variablelist> + </para> + <para> +Some of these rules require an extra <varname>format</varname> parameter in order to work correctly. For example, the <varname>regex</varname> rule can be used this way to validate a signed integer: +<programlisting role="php"> +<![CDATA[ +$form->addRule('amount', 'Amount incorrect', 'regex', '/^[+-]?\d+$/'); +]]> +</programlisting> + </para> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.functions"> + <title>Validation functions and methods</title> + <para> +When you need a more complex validation, QuickForm can use your own custom-made functions to validate an element or a group. QuickForm can also call a method inside a class. This way, it is possible to use PEAR's <classname>Validate</classname> package or any other class. If you want to use your own functions, QuickForm needs a way to find them, so they have to be registered first. Validation with function is done even if the element is not required. + </para> +<example> +<title>Email validation function</title> +<programlisting role="php"> +<![CDATA[ +/** + * Validate an email address + * + * @param string $email Email address to validate + * @param boolean $domainCheck Check if the domain exists + */ +function checkEmail($email, $domainCheck = false) +{ + if (preg_match('/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+'. + '\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)) { + if ($domainCheck && function_exists('checkdnsrr')) { + list (, $domain) = explode('@', $email); + if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) { + return true; + } + return false; + } + return true; + } + return false; +} +$form->registerRule('checkmail', 'function', 'checkEmail'); +$form->addRule('email', 'Email is incorrect', 'checkmail', true); +]]> +</programlisting> +</example> + <para> +You can pass an extra parameter of the type you want to your function when set with <function>HTML_QuickForm::addRule</function>. Here we used &true; to enable the DNS check of our function. + </para> + <para> +If you use a method, you must specify the class your method is in. Use this syntax when you register your rule: +<programlisting role="php"> +<![CDATA[ +// Method checkEmail is in class Validate +$form->registerRule('checkmail', 'function', 'checkEmail', 'Validate'); +]]> +</programlisting> + </para> + <tip><para> +You can also use a javascript function to validate your form, give it the same name as your PHP function, have it return a boolean and set the <parameter>'client'</parameter> parameter. + </para></tip> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.groups"> + <title>Group validation</title> + <para> +Groups of elements can be validated the same way other elements are, or use a more complex validation scheme. To validate a group as a whole, just use <function>HTML_QuickForm::addRule</function>. All your elements will then be validated one after the other using the same rule. If you use a validation function, every element values will be passed to the function as an array. If you use client-side validation, element values will be concatenated before being validated. + </para> + <para> +You can have more complex validation for your groups using the <function>HTML_QuickForm::addGroupRule</function> method. This allows for a per element validation. It also makes it possible to specify the number of elements that need to be valid for the group to be valid too. + </para> +<example> + <title>Complex group validation</title> +<programlisting role="php"> +<![CDATA[ +// Group +$id[] = &HTML_QuickForm::createElement('text', 'username', 'Username'); +$id[] = &HTML_QuickForm::createElement('text', 'code', 'Code'); +$form->addGroup($id, 'id', 'Your ID:', '-'); + +// Validation rules per element + +$rule['username'][] = array('Username is required', 'required'); +$rule['username'][] = array('Username contains only letters', 'lettersonly'); +$rule['username'][] = array('Username has between 5-8 characters', 'rangelength', '5,8'); + +$rule['code'][] = array('Code is required', 'required'); +$rule['code'][] = array('Code contains numbers only', 'regex', '/^\d+$/'); +$rule['code'][] = array('Code is 5 numbers long', 'rangelength', '5'); + +$form->addGroupRule('id', $rule); +]]> +</programlisting> +</example> + <para> +In this example, we have set rules for the elements inside our group. Instead of using their names, we could have used their index (determined by the order they were created) in the group, it would speed up the validation process. + </para> + <para> +The following example takes the same group and will validate the form only if at least one of our two elements is not empty. To acheive this, we use the <parameter>howmany</parameter> parameter and set it to 1. +<programlisting role="php"> +<![CDATA[ +$form->addGroupRule('id', 'Fill at least one element', 'required', null, 1); +]]> +</programlisting> + </para> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.conclusion"> + <title>Conclusion</title> + <para> +You have seen that QuickForm makes it easy to validate your elements and groups without having to write all the usually necessary code to find the different values. It takes care of required elements, generates the javascript automatically and adds a lot of flexibility by allowing you to use your own validation functions and regular expressions. It's time to experiment... + </para> + </refsect2> </refsect1> <refsect1 id="package.html.html-quickform.intro-validation.filters"> <title>Filters</title>
Index: peardoc/en/package/html/html-quickform/intro-validation.xml diff -u peardoc/en/package/html/html-quickform/intro-validation.xml:1.1 peardoc/en/package/html/html-quickform/intro-validation.xml:1.2 --- peardoc/en/package/html/html-quickform/intro-validation.xml:1.1 Fri May 23 10:21:32 2003 +++ peardoc/en/package/html/html-quickform/intro-validation.xml Wed May 28 11:30:33 2003 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <refentry id="package.html.html-quickform.intro-validation"> <refnamediv> <refname>Introduction - validation and filters</refname> @@ -7,9 +7,212 @@ <refsect1 id="package.html.html-quickform.intro-validation.rules"> <title>Validation rules</title> <para> - To be converted +QuickForm makes client-side and server-side form validation easy. It allows for validation against regular +expressions or custom functions and methods. You can define your own validation rules and apply them to +the elements or groups you want. In this section, we will explore the different possibilities QuickForm +offers to make validation easier. + </para> + <para> +QuickForm can verify if required elements are filled when the form is submitted. This works with every type of elements or groups. QuickForm uses an internal regular expression to check if the element is empty. The 0 integer is not considered as an empty value. The fact that an element is required triggers the use of the other validation rules set for that same element. +<programlisting role="php"> +<![CDATA[ +require_once 'HTML/QuickForm.php'; + +$form = new HTML_QuickForm('myform', 'post'); +$form->addElement('text', 'email', 'Your email:'); +$form->addElement('submit', 'submit', 'Submit'); + +// Validation rules + +$form->addRule('email', 'E-Mail is required', 'required'); + +// Validation + +if ($form->validate()) { + $form->freeze(); +} +$form->display(); +]]> +</programlisting> + </para> + <para> +The <function>HTML_QuickForm::validate</function> method will scan through each rules in the order they have been set. If a rule is not validated, the error message corresponding to the element will be displayed and the form will be shown once again. You can use templates to set the position of this error message. The order you decide to set your validation rules is important because it will determine which error message is used. </para> + <refsect2 id="package.html.html-quickform.intro-validation.rules.client"> + <title>Client-side validation</title> + <para> +QuickForm can generate the javascript necessary to validate the form on the client side. This feature works for every standard elements. Groups will be validated on the client side by concatenating all grouped elements value into a single value. Server side validation is always performed in case the client has javascript turned off. +<programlisting role="php"> +<![CDATA[ +$form->addRule('email', 'E-Mail is required', 'required', null, 'client'); +]]> +</programlisting> + </para> + <tip><para> +By setting the parameter 'client', you trigger the javascript automatic generation. + </para></tip> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.builtin"> + <title>Built-in validation rules</title> + <para> +QuickForm offers a few registered rules that are often used when validating forms. These actually are regular expressions ready to use, 'required' is one of those. + <variablelist> + <varlistentry> + <term><varname>required</varname></term> + <listitem><para>value is not empty</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>maxlength</varname></term> + <listitem><para>value must not exceed <parameter>n</parameter> characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>minlength</varname></term> + <listitem><para>value must have more than <parameter>n</parameter> characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>rangelength</varname></term> + <listitem><para>value must have between <parameter>m</parameter> and <parameter>n</parameter> characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>regex</varname></term> + <listitem><para>value must pass the regular expression</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>email</varname></term> + <listitem><para>value is a correctly formatted email</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>lettersonly</varname></term> + <listitem><para>value must contain only letters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>alphanumeric</varname></term> + <listitem><para>value must contain only letters and numbers</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>numeric</varname></term> + <listitem><para>value must be a number</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>nopunctuation</varname></term> + <listitem><para>value must not contain punctuation characters</para></listitem> + </varlistentry> + <varlistentry> + <term><varname>nonzero</varname></term> + <listitem><para>value must be a number not starting with 0</para></listitem> + </varlistentry> + </variablelist> + </para> + <para> +Some of these rules require an extra <varname>format</varname> parameter in order to work correctly. For example, the <varname>regex</varname> rule can be used this way to validate a signed integer: +<programlisting role="php"> +<![CDATA[ +$form->addRule('amount', 'Amount incorrect', 'regex', '/^[+-]?\d+$/'); +]]> +</programlisting> + </para> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.functions"> + <title>Validation functions and methods</title> + <para> +When you need a more complex validation, QuickForm can use your own custom-made functions to validate an element or a group. QuickForm can also call a method inside a class. This way, it is possible to use PEAR's <classname>Validate</classname> package or any other class. If you want to use your own functions, QuickForm needs a way to find them, so they have to be registered first. Validation with function is done even if the element is not required. + </para> +<example> +<title>Email validation function</title> +<programlisting role="php"> +<![CDATA[ +/** + * Validate an email address + * + * @param string $email Email address to validate + * @param boolean $domainCheck Check if the domain exists + */ +function checkEmail($email, $domainCheck = false) +{ + if (preg_match('/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+'. + '\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)) { + if ($domainCheck && function_exists('checkdnsrr')) { + list (, $domain) = explode('@', $email); + if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) { + return true; + } + return false; + } + return true; + } + return false; +} +$form->registerRule('checkmail', 'function', 'checkEmail'); +$form->addRule('email', 'Email is incorrect', 'checkmail', true); +]]> +</programlisting> +</example> + <para> +You can pass an extra parameter of the type you want to your function when set with <function>HTML_QuickForm::addRule</function>. Here we used &true; to enable the DNS check of our function. + </para> + <para> +If you use a method, you must specify the class your method is in. Use this syntax when you register your rule: +<programlisting role="php"> +<![CDATA[ +// Method checkEmail is in class Validate +$form->registerRule('checkmail', 'function', 'checkEmail', 'Validate'); +]]> +</programlisting> + </para> + <tip><para> +You can also use a javascript function to validate your form, give it the same name as your PHP function, have it return a boolean and set the <parameter>'client'</parameter> parameter. + </para></tip> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.groups"> + <title>Group validation</title> + <para> +Groups of elements can be validated the same way other elements are, or use a more complex validation scheme. To validate a group as a whole, just use <function>HTML_QuickForm::addRule</function>. All your elements will then be validated one after the other using the same rule. If you use a validation function, every element values will be passed to the function as an array. If you use client-side validation, element values will be concatenated before being validated. + </para> + <para> +You can have more complex validation for your groups using the <function>HTML_QuickForm::addGroupRule</function> method. This allows for a per element validation. It also makes it possible to specify the number of elements that need to be valid for the group to be valid too. + </para> +<example> + <title>Complex group validation</title> +<programlisting role="php"> +<![CDATA[ +// Group +$id[] = &HTML_QuickForm::createElement('text', 'username', 'Username'); +$id[] = &HTML_QuickForm::createElement('text', 'code', 'Code'); +$form->addGroup($id, 'id', 'Your ID:', '-'); + +// Validation rules per element + +$rule['username'][] = array('Username is required', 'required'); +$rule['username'][] = array('Username contains only letters', 'lettersonly'); +$rule['username'][] = array('Username has between 5-8 characters', 'rangelength', '5,8'); + +$rule['code'][] = array('Code is required', 'required'); +$rule['code'][] = array('Code contains numbers only', 'regex', '/^\d+$/'); +$rule['code'][] = array('Code is 5 numbers long', 'rangelength', '5'); + +$form->addGroupRule('id', $rule); +]]> +</programlisting> +</example> + <para> +In this example, we have set rules for the elements inside our group. Instead of using their names, we could have used their index (determined by the order they were created) in the group, it would speed up the validation process. + </para> + <para> +The following example takes the same group and will validate the form only if at least one of our two elements is not empty. To acheive this, we use the <parameter>howmany</parameter> parameter and set it to 1. +<programlisting role="php"> +<![CDATA[ +$form->addGroupRule('id', 'Fill at least one element', 'required', null, 1); +]]> +</programlisting> + </para> + </refsect2> + <refsect2 id="package.html.html-quickform.intro-validation.rules.conclusion"> + <title>Conclusion</title> + <para> +You have seen that QuickForm makes it easy to validate your elements and groups without having to write all the usually necessary code to find the different values. It takes care of required elements, generates the javascript automatically and adds a lot of flexibility by allowing you to use your own validation functions and regular expressions. It's time to experiment... + </para> + </refsect2> </refsect1> <refsect1 id="package.html.html-quickform.intro-validation.filters"> <title>Filters</title>