Zebra_Form 是一个 PHP 类用于简化表单的创建和数据验证。示例代码:
项目地址:
01 | <?php |
02 | // include the Zebra_Form class |
03 | require 'path/to/Zebra_Form.php' ; |
04 |
05 | // instantiate a Zebra_Form object |
06 | $form = new Zebra_Form( 'form' ); |
07 |
08 | // the label for the "email" field |
09 | $form ->add( 'label' , 'label_email' , 'email' , 'Email' ); |
10 |
11 | // add the "email" field |
12 | // the "&" symbol is there so that $obj will be a reference to the object in PHP 4 |
13 | // for PHP 5+ there is no need for it |
14 | $obj = & $form ->add( 'text' , 'email' , '' , array ( 'autocomplete' => 'off' )); |
15 |
16 | // set rules |
17 | $obj ->set_rule( array ( |
18 |
// error messages will be sent to a variable called "error", usable in custom templates |
19 |
'required' => array ( 'error' , 'Email is required!' ), |
20 |
'email' => array ( 'error' , 'Email address seems to be invalid!' ), |
21 | )); |
22 |
23 | // "password" |
24 | $form ->add( 'label' , 'label_password' , 'password' , 'Password' ); |
25 | $obj = & $form ->add( 'password' , 'password' , '' , array ( 'autocomplete' => 'off' )); |
26 | $obj ->set_rule( array ( |
27 |
'required' => array ( 'error' , 'Password is required!' ), |
28 |
'length' => array (6, 10, 'error' , 'The password must have between 6 and 10 characters' ), |
29 | )); |
30 |
31 | // "remember me" |
32 | $form ->add( 'checkbox' , 'remember_me' , 'yes' ); |
33 | $form ->add( 'label' , 'label_remember_me_yes' , 'remember_me_yes' , 'Remember me' ); |
34 |
35 | // "submit" |
36 | $form ->add( 'submit' , 'btnsubmit' , 'Submit' ); |
37 |
38 | // validate the form |
39 | if ( $form ->validate()) { |
40 |
// do stuff here |
41 | } |
42 | // auto generate output, labels above form elements |
43 | $form ->render(); |
44 | ?> |