| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
?> |
|---|
| 16 |
<html> |
|---|
| 17 |
<head> |
|---|
| 18 |
<title>formValidator example</title> |
|---|
| 19 |
<style> |
|---|
| 20 |
@import url( _styles.css ); |
|---|
| 21 |
</style> |
|---|
| 22 |
</head> |
|---|
| 23 |
<body> |
|---|
| 24 |
|
|---|
| 25 |
<form name="myForm" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="get"> |
|---|
| 26 |
|
|---|
| 27 |
<?php |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
* main patForms class |
|---|
| 31 |
*/ |
|---|
| 32 |
require_once $neededFiles['patForms']; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
* patErrorManager class |
|---|
| 36 |
*/ |
|---|
| 37 |
require_once $neededFiles['patErrorManager']; |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
* Helper functions needed by the patForms example files |
|---|
| 41 |
*/ |
|---|
| 42 |
include_once( "_helperfunctions.php" ); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
include dirname( __FILE__ ) . '/setup_gettext.php'; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
$username = array( |
|---|
| 50 |
"required" => "yes", |
|---|
| 51 |
"display" => "yes", |
|---|
| 52 |
"edit" => "no", |
|---|
| 53 |
"label" => "Username", |
|---|
| 54 |
"description" => "Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]", |
|---|
| 55 |
"position" => "1", |
|---|
| 56 |
"accesskey" => "A", |
|---|
| 57 |
"default" => "argh", |
|---|
| 58 |
"maxlength" => "15", |
|---|
| 59 |
"minlength" => "4", |
|---|
| 60 |
"title" => "Username", |
|---|
| 61 |
"onchange" => "alert( pfe_username.getValue() );" |
|---|
| 62 |
); |
|---|
| 63 |
|
|---|
| 64 |
$email = array( |
|---|
| 65 |
"required" => "yes", |
|---|
| 66 |
"display" => "yes", |
|---|
| 67 |
"edit" => "yes", |
|---|
| 68 |
"label" => "Email", |
|---|
| 69 |
"description" => "Enter your email address here.", |
|---|
| 70 |
"position" => "1", |
|---|
| 71 |
"accesskey" => "A", |
|---|
| 72 |
"default" => "argh@php-tools.net", |
|---|
| 73 |
"maxlength" => "50", |
|---|
| 74 |
"minlength" => "4", |
|---|
| 75 |
"format" => "email", |
|---|
| 76 |
"title" => "Email", |
|---|
| 77 |
"onchange" => "alert( pfe_email.getValue() );" |
|---|
| 78 |
); |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
$pub = array( |
|---|
| 82 |
'name' => 'publisher', |
|---|
| 83 |
'required' => 'yes', |
|---|
| 84 |
'display' => 'yes', |
|---|
| 85 |
'edit' => 'yes', |
|---|
| 86 |
'label' => 'Favorite publisher', |
|---|
| 87 |
'description' => 'Choose your favorite comics publisher', |
|---|
| 88 |
'position' => '1', |
|---|
| 89 |
'accesskey' => 'p', |
|---|
| 90 |
'values' => array( |
|---|
| 91 |
array( |
|---|
| 92 |
'label' => 'Please choose...', |
|---|
| 93 |
'value' => '' |
|---|
| 94 |
), |
|---|
| 95 |
array( |
|---|
| 96 |
'label' => 'DC Comics', |
|---|
| 97 |
'value' => 'dc' |
|---|
| 98 |
), |
|---|
| 99 |
array( |
|---|
| 100 |
'label' => 'Marvel Comics', |
|---|
| 101 |
'value' => 'marvel' |
|---|
| 102 |
) |
|---|
| 103 |
), |
|---|
| 104 |
"onchange" => "alert( pfe_publisher.getValue() );" |
|---|
| 105 |
); |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
$form = patForms::createForm(); |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
$form->setDefaultAttributes( array( "style" => "width:100%;" ) ); |
|---|
| 113 |
|
|---|
| 114 |
$form->setAttribute( 'name', 'myForm' ); |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
//$form->setMode( "readonly" ); |
|---|
| 118 |
|
|---|
| 119 |
// create needed elements |
|---|
| 120 |
$el1 = $form->createElement( "user", "String", $username ); |
|---|
| 121 |
$el2 = $form->createElement( "email", "String", $email ); |
|---|
| 122 |
$el3 = $form->createElement( 'publisher', 'Enum', $pub ); |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
$form->addElement( $el1 ); |
|---|
| 126 |
$form->addElement( $el2 ); |
|---|
| 127 |
$form->addElement( $el3 ); |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
if( isset( $_GET["save"] ) ) |
|---|
| 131 |
{ |
|---|
| 132 |
|
|---|
| 133 |
$form->setSubmitted( true ); |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
if( !$form->validateForm() ) |
|---|
| 137 |
{ |
|---|
| 138 |
echo "<b>patForms:</b> validation failed. Errors:<br><br>"; |
|---|
| 139 |
|
|---|
| 140 |
$errors = $form->getValidationErrors(); |
|---|
| 141 |
|
|---|
| 142 |
foreach( $errors as $elementName => $elementErrors ) |
|---|
| 143 |
{ |
|---|
| 144 |
if( empty( $elementErrors ) ) |
|---|
| 145 |
continue; |
|---|
| 146 |
|
|---|
| 147 |
echo 'Field: <b>'.$elementName.'</b>'; |
|---|
| 148 |
echo "<pre>"; |
|---|
| 149 |
print_r( $elementErrors ); |
|---|
| 150 |
echo "</pre>"; |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
else |
|---|
| 154 |
echo "<b>patForms:</b> validation successful.<br><br>"; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
$renderer =& patForms::createRenderer( "Array" ); |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
$form->setRenderer( $renderer ); |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
$elements = $form->renderForm(); |
|---|
| 165 |
|
|---|
| 166 |
echo $form->getScripts(); |
|---|
| 167 |
|
|---|
| 168 |
?> |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
<!-- DISPLAY SERIALIZED ELEMENTS ------------------------------------------------------ --> |
|---|
| 172 |
<fieldset><legend><b>Generated elements</b></legend> |
|---|
| 173 |
<?php |
|---|
| 174 |
|
|---|
| 175 |
foreach( $elements as $element ) |
|---|
| 176 |
{ |
|---|
| 177 |
echo $element["label"]."<br>"; |
|---|
| 178 |
echo "<div>".$element["element"]."</div>"; |
|---|
| 179 |
echo "<i>".$element["description"]."</i><br><br>"; |
|---|
| 180 |
} |
|---|
| 181 |
?> |
|---|
| 182 |
</fieldset><br> |
|---|
| 183 |
|
|---|
| 184 |
<input type="submit" name="save" value="Save form"/><br><br> |
|---|
| 185 |
|
|---|
| 186 |
<fieldset><legend><b>Javascript output</b></legend> |
|---|
| 187 |
<pre> |
|---|
| 188 |
<?php |
|---|
| 189 |
echo htmlspecialchars( $form->getScripts() ); |
|---|
| 190 |
?> |
|---|
| 191 |
</pre> |
|---|
| 192 |
</fieldset><br> |
|---|
| 193 |
|
|---|
| 194 |
<!-- SOURCE CODE OUTPUT -------------------------------------------------------------- --> |
|---|
| 195 |
<fieldset><legend><b>Elements source code</b></legend> |
|---|
| 196 |
<?php |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
foreach( $elements as $element ) |
|---|
| 200 |
{ |
|---|
| 201 |
displaySource( $element['element'] ); |
|---|
| 202 |
echo '<br>'; |
|---|
| 203 |
} |
|---|
| 204 |
?> |
|---|
| 205 |
</fieldset><br> |
|---|
| 206 |
|
|---|
| 207 |
</form> |
|---|
| 208 |
</body> |
|---|
| 209 |
</html> |
|---|
| 210 |
|
|---|