Form Validation
Example showing how to add Custom validation to RaxanDataSanitizer
PHP Source:
<?php
require_once("raxan/pdi/autostart.php");
// add custom phone and money validators
$san = Raxan::dataSanitizer();
$san->addDataValidator('Phone', '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/'); // using regex
$san->addDataValidator('ZipCode','validate_ZipCode'); // using callback
function validate_ZipCode($value){ // validate zip code
return preg_match('/^(\d{5}-\d{4})|(\d{5})$/',$value);
}
class MyPage extends RaxanWebPage {
protected $frm;
protected $preserveFormContent = true; // redisplay form values
protected function _load() {
// load the html page
$this->source('views/update-form.html');
// bind to form submit event
$this->frm = $this['form']->bind('submit', '.form_submit');
}
protected function form_submit($e) { // event callback
$msg = array();
$request = $this->clientRequest();
// validate user input
if (!$request->isPhone('phone')) $msg[] ='Please enter a valid phone number (format: 123-123-1234).';
if (!$request->isZipCode('zipcode')) $msg[] ='Please enter a valid US Zip code.';
if (count($msg)>0) {
$msg = '<strong>'.implode('<br />',$msg).'</strong>';
$this['#msg']->html($msg)->show();
}
}
}
?>
HTML Source (views/update-form.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Update Form</title>
<link href="../raxan/styles/master.css" rel="stylesheet" type="text/css" />
<!--[if IE]><link rel="stylesheet" href="../raxan/styles/master.ie.css" type="text/css"><![endif]-->
<style type="text/css">
label {display:block; float:left;width:120px}
</style>
</head>
<body>
<div class="container c35">
<h2>Update Information</h2>
<form class="c20" method="post">
<fieldset>
<div id="msg" class="notice hide bmm"></div>
<label>Phone number:</label><input type="text" name="phone" class="textbox c10" /><br />
<label>Zip code:</label><input type="text" name="zipcode" class="textbox c10" /><br />
<hr />
<input type="submit" value="Submit" />
</fieldset>
</form>
</div>
</body>
</html>