API Allow actions to declare they are exempt from validation themselves

This commit is contained in:
Hamish Friedlander 2016-04-22 14:21:43 +12:00
parent dba0247544
commit 241cdfed1b
2 changed files with 41 additions and 2 deletions

View File

@ -712,6 +712,18 @@ class Form extends RequestHandler {
return $this->validationExemptActions;
}
/**
* Passed a FormAction, returns true if that action is exempt from Form validation
*
* @param FormAction $action
* @return bool
*/
public function actionIsValidationExempt($action) {
if ($action->getValidationExempt()) return true;
if (in_array($action->actionName(), $this->getValidationExemptActions())) return true;
return false;
}
/**
* Convert this form to another format.
* @param FormTransformation $format
@ -1342,8 +1354,8 @@ class Form extends RequestHandler {
* @return boolean
*/
public function validate(){
$buttonClicked = $this->buttonClicked();
if($buttonClicked && in_array($buttonClicked->actionName(), $this->getValidationExemptActions())) {
$action = $this->buttonClicked();
if($action && $this->actionIsValidationExempt($action)) {
return true;
}

View File

@ -39,6 +39,13 @@ class FormAction extends FormField {
*/
protected $buttonContent = null;
/**
* Should validation be skipped when performing this action?
*
* @var bool
*/
protected $validationExempt = false;
/**
* Create a new action button.
*
@ -173,6 +180,26 @@ class FormAction extends FormField {
return $this->useButtonTag;
}
/**
* Set whether this action can be performed without validating the data
*
* @param bool $exempt
* @return $this
*/
public function setValidationExempt($exempt = true) {
$this->validationExempt = $exempt;
return $this;
}
/**
* Get whether this action can be performed without vaidating the data
*
* @return bool
*/
public function getValidationExempt() {
return $this->validationExempt;
}
/**
* Does not transform to readonly by purpose.
* Globally disabled buttons would break the CMS.