mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Using Validator::getJavascriptValidationHandler() to determine instance settings, and fall back to static defaults in Validator->includeJavascriptValidation(), instead of setting instance-specific parameters in __construct() calls. This means Validator.js won't be included if handlers are globally disabled.
ENHANCEMENT Added Validator::getJavascriptValidationHandler() API CHANGE Deprecated Validator::showErrors() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@72373 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
7f4f57c307
commit
8f0979eb78
@ -14,34 +14,64 @@
|
|||||||
* @subpackage validators
|
* @subpackage validators
|
||||||
*/
|
*/
|
||||||
abstract class Validator extends Object {
|
abstract class Validator extends Object {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Form $form
|
||||||
|
*/
|
||||||
protected $form;
|
protected $form;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $errors
|
||||||
|
*/
|
||||||
protected $errors;
|
protected $errors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static for default value of $this->javascriptValidationHandler.
|
* Static for default value of $this->javascriptValidationHandler.
|
||||||
* Set with Validator::set_javascript_validation_handler();
|
* Set with Validator::set_javascript_validation_handler();
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected static $javascript_validation_handler = null;
|
protected static $javascript_validation_handler = "prototype";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for javascript validation. Can be "prototype" or "none"
|
* Handler for javascript validation. Can be "prototype" or "none".
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $javascriptValidationHandler = "prototype";
|
protected $javascriptValidationHandler = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call this function to set the javascript validation handler for all valdiation on your site.
|
* 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()
|
* This could be called from _config.php to set site-wide javascript validation, or from ContentController::init()
|
||||||
* to affect only the front-end site.
|
* to affect only the front-end site.
|
||||||
|
* Use instance method {@link setJavascriptValidationHandler()} to
|
||||||
|
* only set handler for a specific form instance.
|
||||||
*
|
*
|
||||||
* @param $handler A string representing the handler to use: 'prototype' or 'none'.
|
* @param $handler A string representing the handler to use: 'prototype' or 'none'.
|
||||||
* @todo Add 'jquery' as a handler option.
|
* @todo Add 'jquery' as a handler option.
|
||||||
*/
|
*/
|
||||||
public function set_javascript_validation_handler($handler = 'prototype') {
|
public static function set_javascript_validation_handler($handler) {
|
||||||
|
if($handler == 'prototype' || $handler == 'none') {
|
||||||
self::$javascript_validation_handler = $handler;
|
self::$javascript_validation_handler = $handler;
|
||||||
|
} else {
|
||||||
|
user_error("Validator::setJavascriptValidationHandler() passed bad handler '$handler'", E_USER_WARNING);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disable JavaScript validation for this validator
|
* Returns global validation handler used for all forms by default,
|
||||||
|
* unless overwritten by {@link setJavascriptValidationHandler()}.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function get_javascript_validator_handler() {
|
||||||
|
return self::$javascript_validation_handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set JavaScript validation for this validator.
|
||||||
|
* Use static method {@link set_javascript_validation_handler()}
|
||||||
|
* to set handlers globally.
|
||||||
|
*
|
||||||
|
* @param string $handler
|
||||||
*/
|
*/
|
||||||
public function setJavascriptValidationHandler($handler) {
|
public function setJavascriptValidationHandler($handler) {
|
||||||
if($handler == 'prototype' || $handler == 'none') {
|
if($handler == 'prototype' || $handler == 'none') {
|
||||||
@ -51,19 +81,26 @@ abstract class Validator extends Object {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct() {
|
/**
|
||||||
if(self::$javascript_validation_handler) $this->setJavascriptValidationHandler(self::$javascript_validation_handler);
|
* Gets the current javascript validation handler for this form.
|
||||||
|
* If not set, falls back to the global static {@link self::$javascript_validation_handler}.
|
||||||
if($this->javascriptValidationHandler && $this->javascriptValidationHandler != 'none') {
|
*
|
||||||
Requirements::javascript(SAPPHIRE_DIR . '/javascript/Validator.js');
|
* @return string
|
||||||
}
|
*/
|
||||||
parent::__construct();
|
public function getJavascriptValidationHandler() {
|
||||||
|
return ($this->javascriptValidationHandler) ? $this->javascriptValidationHandler : self::$javascript_validation_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Form $form
|
||||||
|
*/
|
||||||
function setForm($form) {
|
function setForm($form) {
|
||||||
$this->form = $form;
|
$this->form = $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array Errors (if any)
|
||||||
|
*/
|
||||||
function validate(){
|
function validate(){
|
||||||
$this->errors = null;
|
$this->errors = null;
|
||||||
$this->php($this->form->getData());
|
$this->php($this->form->getData());
|
||||||
@ -86,6 +123,9 @@ abstract class Validator extends Object {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 2.4 Use Validator->getCombinedError() and custom code
|
||||||
|
*/
|
||||||
function showError() {
|
function showError() {
|
||||||
Debug::show($this->errors);
|
Debug::show($this->errors);
|
||||||
}
|
}
|
||||||
@ -109,10 +149,11 @@ abstract class Validator extends Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function includeJavascriptValidation() {
|
function includeJavascriptValidation() {
|
||||||
if($this->javascriptValidationHandler == 'prototype') {
|
if($this->getJavascriptValidationHandler() == 'prototype') {
|
||||||
Requirements::javascript(THIRDPARTY_DIR . "/prototype.js");
|
Requirements::javascript(THIRDPARTY_DIR . "/prototype.js");
|
||||||
Requirements::javascript(THIRDPARTY_DIR . "/behaviour.js");
|
Requirements::javascript(THIRDPARTY_DIR . "/behaviour.js");
|
||||||
Requirements::javascript(THIRDPARTY_DIR . "/prototype_improvements.js");
|
Requirements::javascript(THIRDPARTY_DIR . "/prototype_improvements.js");
|
||||||
|
//Requirements::add_i18n_javascript(SAPPHIRE_DIR);
|
||||||
Requirements::javascript(SAPPHIRE_DIR . "/javascript/i18n.js");
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/i18n.js");
|
||||||
Requirements::javascript(SAPPHIRE_DIR . "/javascript/Validator.js");
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/Validator.js");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user