@ -26,122 +26,243 @@ class FormValidationException extends Exception {
}
}
}
}
class CPHPFormValidator
class ImmediateAbort extends FormValidationException { }
class CPHPFormValidatorPromiseBaseClass
{
{
public function GenerateResultObject($failed, $critical)
public $previous = null;
public $next = null;
public function __construct($creator)
{
{
if($failed === true && $critical === true)
$this->previous = $creator;
}
public function StartResolve()
{
/* Back and forth! */
if(is_null($this->previous) === true)
{
{
$object = new CPHPFormValidatorAbort();
$this->next->ContinueResolve(array());
}
}
else
else
{
{
$object = new CPHPFormValidatorResult();
$this->previous->StartResolve();
}
}
public function ContinueResolve($results)
{
$own_result = $this->Resolve($results);
if(is_null($own_result) === false)
{
$results[] = $own_result;
}
}
$object->handler = $this->handler;
if(is_null($this->next) === false)
return $object;
{
$this->next->ContinueResolve($results);
}
else
{
$this->ValidationFinished($results);
}
}
public function ValidationFinished($results)
{
if(count($results) > 0)
{
throw new FormValidationException("One or more validation steps failed.", $results);
}
}
}
public function Done()
{
/* Trigger validation routine */
try
{
$this->StartResolve();
}
catch (ImmediateAbort $e)
{
throw new FormValidationException("A critical validation step failed.", $e->exceptions);
}
}
/* Operators */
public function Either($error_message)
{
$this->next = new CPHPFormValidatorOperatorEither($this, $error_message, array_slice(func_get_args(), 1));
return $this->next;
}
public function All($error_message)
{
$this->next = new CPHPFormValidatorOperatorAll($this, $error_message, array_slice(func_get_args(), 1));
return $this->next;
}
/* Validators */
public function RequireKey($key, $critical = false)
public function RequireKey($key, $critical = false)
{
{
return $this->ProcessSingleValidationResult($key, (!isset($this->handler->formdata[$key])), "required", "A value is required for this field.", $critical);
$this->next = new CPHPFormValidatorPromise($this, $this->handler, $key, array(), "required", "A value is required for this field.", $critical, function($key, $value, $args, $handler){
return isset($handler->formdata[$key]);
});
return $this->next;
}
}
public function ValidateEmail($key, $critical = false)
public function ValidateEmail($key, $critical = false)
$this->next = new CPHPFormValidatorPromise($this, $this->handler, $key, array(), "email", "The value is not a valid e-mail address.", $critical, function($key, $value, $args, $handler){