Commit a3c80462 authored by jaapjansma's avatar jaapjansma

Input type also passed when using default values

parent fa178627
......@@ -139,7 +139,7 @@ class FormProcessorDefaults extends FormProcessor implements API_ProviderInterfa
// Check the validations on the input.
if (isset($params[$input['name']]) && $params[$input['name']] != "") {
foreach ($input['validators'] as $validator) {
if (!$validator['validator']->validate($params[$input['name']])) {
if (!$validator['validator']->validate($params[$input['name']], $inputType)) {
throw new \API_Exception($validator['validator']->getInvalidMessage() . ' (Parameter ' . $input['name'] . ')');
}
}
......
......@@ -4,49 +4,50 @@
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
namespace Civi\FormProcessor\Validation;
use \Civi\FormProcessor\Config\ConfigurationBag;
use \Civi\FormProcessor\Config\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
use CRM_FormProcessor_ExtensionUtil as E;
abstract class AbstractValidator implements \JsonSerializable {
/**
* @var ConfigurationBag
*/
protected $configuration;
/**
* @var ConfigurationBag
*/
protected $defaultConfiguration;
/**
* Returns the name of the validator.
*
*
* @return string
*/
abstract public function getName();
/**
* Returns the label of the validator.
*
*
* @return string
*/
abstract public function getLabel();
/**
* Validate the input.
*
*
* @param mixed $input
* @param \Civi\FormProcessor\Type\AbstractType $inputType
* @return bool
*/
abstract public function validate($input, $inputType);
public function __construct() {
$this->configuration = new ConfigurationBag();
$this->defaultConfiguration = new ConfigurationBag();
......@@ -58,16 +59,16 @@
}
}
}
/**
* Get the configuration specification
*
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
/**
* @return bool;
*/
......@@ -77,31 +78,31 @@
}
return SpecificationBag::validate($this->configuration, $this->getConfigurationSpecification());
}
/**
* @return ConfigurationBag
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* @return ConfigurationBag
*/
public function getDefaultConfiguration() {
return $this->defaultConfiguration;
}
/**
* Returns the invalid message.
*
*
* @return string
*/
public function getInvalidMessage() {
return E::ts('Invalid %1', array(1=>$this->getLabel()));
}
/**
* @param ConfigurationBag $configuration
*/
......@@ -109,7 +110,7 @@
$this->configuration = $configuration;
return $this;
}
public function toArray() {
return array(
'name' => $this->getName(),
......@@ -119,9 +120,9 @@
'configuration' => $this->getConfiguration()->toArray(),
);
}
public function jsonSerialize() {
return $this->toArray();
}
}
......@@ -85,7 +85,7 @@ class DateValidator extends AbstractValidator {
* Validate the input.
*
* @param mixed $input
*
* @param \Civi\FormProcessor\Type\AbstractType $inputType
* @return bool
*/
public function validate($input, $inputType) {
......
......@@ -4,39 +4,40 @@
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
namespace Civi\FormProcessor\Validation;
use \Civi\FormProcessor\Validation\AbstractValidator;
use CRM_FormProcessor_ExtensionUtil as E;
class EmailValidator extends AbstractValidator {
const PATTERN_LOOSE = '/^.+\@\S+\.\S+$/';
public function getLabel() {
return E::ts('E-mail');
}
/**
* Returns the name of the validator.
*
*
* @return string
*/
public function getName() {
return 'email';
}
/**
* Validate the input.
*
*
* @param mixed $input
* @param \Civi\FormProcessor\Type\AbstractType $inputType
* @return bool
*/
public function validate($input, $inputType) {
$pattern = EmailValidator::PATTERN_LOOSE;
return preg_match($pattern, $input);
}
}
......@@ -39,6 +39,7 @@
* Validate the input.
*
* @param mixed $input
* @param \Civi\FormProcessor\Type\AbstractType $inputType
* @return bool
*/
public function validate($input, $inputType) {
......
......@@ -31,6 +31,7 @@
* Validate the input.
*
* @param mixed $input
* @param \Civi\FormProcessor\Type\AbstractType $inputType
* @return bool
*/
public function validate($input, $inputType) {
......
......@@ -39,6 +39,7 @@
* Validate the input.
*
* @param mixed $input
* @param \Civi\FormProcessor\Type\AbstractType $inputType
* @return bool
*/
public function validate($input, $inputType) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment