javascriptValidationHandler.
* Set with Validator::set_javascript_validation_handler();
*/
protected static $javascript_validation_handler = null;
/**
* Handler for javascript validation. Can be "prototype" or "none"
*/
protected $javascriptValidationHandler = "prototype";
/**
* Call this function to set the javascript validation handler for all valdiation on your site.
* This could be called from _config.php to set site-wide javascript validation, or from ContentController::init()
* to affect only the front-end site.
*
* @param $handler A string representing the handler to use: 'prototype' or 'none'.
* @todo Add 'jquery' as a handler option.
*/
public function set_javascript_validation_handler($handler = 'prototype') {
self::$javascript_validation_handler = $handler;
}
/**
* Disable JavaScript validation for this validator
*/
public function setJavascriptValidationHandler($handler) {
if($handler == 'prototype' || $handler == 'none') {
$this->javascriptValidationHandler = $handler;
} else {
user_error("Validator::setJavascriptValidationHandler() passed bad handler '$handler'", E_USER_WARNING);
}
}
public function __construct() {
if(self::$javascript_validation_handler) $this->setJavascriptValidationHandler(self::$javascript_validation_handler);
if($this->javascriptValidationHandler) {
Requirements::javascript(SAPPHIRE_DIR . '/javascript/Validator.js');
}
parent::__construct();
}
function setForm($form) {
$this->form = $form;
}
function validate(){
$this->errors = null;
$this->php($this->form->getData());
return $this->errors;
}
/**
* Callback to register an error on a field (Called from implementations of {@link FormField::validate})
*
* @param $fieldName name of the field
* @param $message error message to display
* @param $messageType optional parameter, gets loaded into the HTML class attribute in the rendered output
*/
function validationError($fieldName,$message,$messageType=''){
$this->errors[] = array(
'fieldName' => $fieldName,
'message' => $message,
'messageType' => $messageType,
);
}
function showError(){
debug::show($this->errors);
}
function getCombinedError(){
if($this->errors) {
foreach($this->errors as $error){
$ret['message'] .= $error['message']."
";
$ret['messageType'] .= $error['messageType']."
";
}
return $ret;
}
}
function getError(){
return $this->errors;
}
function requireField($fieldName, $data) {
if(!$data[$fieldName]) $this->validationError($fieldName, "$fieldName is required", "required");
}
function includeJavascriptValidation() {
if($this->javascriptValidationHandler == 'prototype') {
Requirements::javascript(THIRDPARTY_DIR . "/prototype.js");
Requirements::javascript(THIRDPARTY_DIR . "/behaviour.js");
Requirements::javascript(THIRDPARTY_DIR . "/prototype_improvements.js");
Requirements::javascript(SAPPHIRE_DIR . "/javascript/Validator.js");
$code = $this->javascript();
$formID = $this->form->FormName();
$js = <<form) $this->form->jsValidationIncluded = true;
}
}
/**
* Returns true if the named field is "required".
* Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template.
* By default, it always returns false.
*/
function fieldIsRequired($fieldName) {
return false;
}
abstract function javascript();
abstract function php($data);
}
?>