Previous Up Next
Zaklady Majax Generovani formularu

Forms Validation

Table of Contents

Formulare - validace

Pokud ma formular nastavenou v html tridu majaxForm, je automaticky pridan mezi formulare, ktere budou validovany. Pokud tuto tridu nema, je mozne ho pridat pomoci funkce Majax.Form.add.

Nasledujici kus kodu ukazuje jednoduchy formular pro pro registraci uzivatele. Je nezbytne, aby mel jednoznacne id.

  1. <table>
  2.     <tr>
  3.         <td>
  4.         <form id="registrationFormA" class="majaxForm" action="">
  5.             <dl>
  6.                 <dt>Username:</dt>
  7.                     <dd><input type="text" name="userName" /></dd>
  8.                 <dt>Password:</dt>    
  9.                     <dd><input type="password" name="newPass1" /></dd>
  10.                 <dt>Confirm Password:</dt>
  11.                     <dd><input type="password" name="newPass2" /></dd>
  12.             </dl>
  13.             <div><input type="submit" value="Send" /></div>
  14.         </form>
  15.         </td>
  16.         <td>
  17.             &nbsp;
  18.         </td>
  19.     </tr>
  20. </table>
Pokud je formular odeslan uzivatelem, je nejdrive zaslan pozadavek na URL specifikovanou v atributu action, kde musi byt funkce, ktera tento pozadavek zpracuje.
  1. require_once('majax.php');
  2.     Majax::Form()->register('registrationFormA''processForm');
  3.  
  4.  
  5.     Majax::Form()->submitOnSuccess(false);
  6.     Majax::process();
  7.     
  8.     
  9.     function processForm($arg)
  10.     {
  11.         $error false;
  12.         if (!$arg['userName']
  13.             Majax::Form()->addAlert('Please, fill username.');
  14.             $error true;
  15.         }
  16.         
  17.         if (!$arg['newPass1'|| !$arg['newPass2']
  18.             Majax::Form()->addAlert('Please, fill password.');
  19.             $error true;
  20.         }
  21.         
  22.         if ($arg['newPass1'!= $arg['newPass2']
  23.             Majax::Form()->addAlert('Passwords are diferent.');
  24.             $error true;
  25.         }
  26.         
  27.         if (strlen($arg['newPass1']8
  28.             Majax::Form()->addAlert('Password too short.');
  29.             $error true;
  30.         }
  31.         
  32.         Majax::Form()->setFormValidity(!$error);
  33.         
  34.         Majax::send();    
  35.         
  36.     
  37.     }

Previous Up Next
Zaklady Majax Generovani formularu

Documentation generated on Tue, 23 Feb 2010 10:23:55 +0100 by phpDocumentor 1.4.3