Example #1
0
 function testRule_SetRuleObject()
 {
     $dataspace = new A_Collection();
     $rules = new A_Rule_Set();
     //$dataspace = new A_Collection();
     $rule = new A_Rule_Notnull(array('field' => 'test', 'errorMsg' => 'error'));
     $rules->addRule($rule);
     $result = $rules->isValid(array());
     $this->assertFalse($result);
     $result = $rules->isValid($this->data);
     $this->assertTrue($result);
 }
Example #2
0
 public function processRequest($request)
 {
     $filterchain = new A_Filter_Set();
     $validator = new A_Rule_Set();
     $this->error = false;
     $param_names = array_keys($this->params);
     if ($param_names) {
         if ($this->filters) {
             foreach ($this->filters as $filter) {
                 // if filter is only for specific params do only those, otherwise all
                 $names = $filter['names'] ? $filter['names'] : $param_names;
                 foreach ($names as $name) {
                     $request->set($name, $filterchain->doFilter($request->get($name), $filter['filter']));
                 }
             }
         }
         foreach ($param_names as $name) {
             if ($this->params[$name]->filters) {
                 $request->set($name, $filterchain->doFilter($request->get($name), $this->params[$name]->filters));
             }
         }
         foreach ($param_names as $name) {
             $this->params[$name]->value = $request->get($name);
             if (isset($this->params[$name]->rules)) {
                 $validator->clearRules();
                 $validator->addRule($this->params[$name]->rules);
                 if (!$validator->isValid($request)) {
                     $errorMsgs = $validator->getErrorMsg();
                     if (isset($errorMsgs[$name])) {
                         $this->params[$name]->setError($errorMsgs[$name]);
                     }
                     $this->error = true;
                 }
             }
         }
         if ($this->rules) {
             foreach ($this->rules as $rule) {
                 // if rule is only for specific params do only those, otherwise all
                 $names = $rule['names'] ? $rule['names'] : $param_names;
                 foreach ($names as $name) {
                     $rule['rule']->setName($name);
                     // set all rules to work on this parameter
                     if (!$rule['rule']->isValid($request)) {
                         $this->params[$name]->setError($rule['rule']->getErrorMsg());
                         $this->error = true;
                     }
                 }
             }
         }
     }
     return !$this->error;
 }
// Iterator:
$alfa = new A_Rule_Alpha();
$validator->addRule(new A_Rule_Iterator($alfa, 'iterator', 'Please fill in a good iterator value'));
// length:
$validator->addRule('A_Rule_Length', 4, null, 'length', 'length must be {min} characters long');
// Match:
$validator->addRule('A_Rule_Match', 'match', 'match2', 'match and match2 must match');
// Notnull:
$validator->addRule('A_Rule_Notnull', 'notnull', 'Fill in something for notnull');
// Numeric:
$validator->addRule('A_Rule_Numeric', 'numeric', 'Fill in a number for numeric');
// Range:
$validator->addRule('A_Rule_Range', 10, 20, 'range', 'Please pick no between 10 and 20');
//Regexp:
$validator->addRule('A_Rule_Regexp', '/^[a-z0-9]+$/D', 'regexp', 'Fill in a correct format for regexp');
if ($validator->isValid($request)) {
    $errmsg = 'OK';
} else {
    $errmsg = print_r($validator->getErrorMsg(), 1);
}
?>
<html>
<head>
<title>Validator Example</title>
</head>
<body>
<h2>Validator Example</h2>

<form action="" method="post">
Status: <span style="color:red"><pre><?php 
echo $errmsg;
Example #4
0
 public function isValid($datasource = null)
 {
     $filterchain = new A_Filter_Set();
     $validator = new A_Rule_Set();
     $validator->excludeRules($this->excludeRules);
     $validator->includeRules($this->includeRules);
     $this->error = false;
     $this->errorMsg = array();
     if ($this->includeRules) {
         $rule_names = $this->includeRules;
     } else {
         $rule_names = array_keys($this->fields);
     }
     if ($this->excludeRules) {
         $rule_names = array_diff($rule_names, $this->excludeRules);
     }
     $field_names = array_keys($this->fields);
     if ($field_names) {
         if (!$datasource) {
             $datasource = $this->datasource;
         }
         // set values from datasource
         foreach ($field_names as $name) {
             if ($datasource->has($name)) {
                 $this->fields[$name]->value = $datasource->get($name);
             }
         }
         // run global filters on all fields
         if ($this->filters) {
             foreach ($this->fields as $field) {
                 $field->value = $filterchain->doFilter($field->value, $this->filters);
             }
         }
         // run field filters
         foreach ($this->fields as $field) {
             if (isset($field->filters)) {
                 $field->value = $filterchain->doFilter($field->value, $field->filters);
             }
         }
         // run rules for each field
         foreach ($this->fields as $name => $field) {
             // clear errors
             $field->setErrorMsg();
             // check if there are rules and if included
             if (isset($field->rules) && in_array($name, $rule_names)) {
                 foreach ($field->rules as $rule) {
                     // check if set to override rule's optional setting
                     if ($field->optional !== null) {
                         $rule->setOptional($field->optional);
                     }
                     $validator->addRule($rule);
                 }
             }
         }
         // check if there are rules and run those as well
         if ($this->rules) {
             foreach ($this->rules as $name => $rule) {
                 $validator->addRule($rule);
             }
         }
         // if the validator is not valid get its errors
         if (!$validator->isValid($datasource)) {
             $this->error = true;
             $errors = $validator->getErrorMsg();
             foreach ($errors as $fieldname => $errorarray) {
                 $this->setErrorMsg($fieldname, $errorarray);
             }
         }
     }
     return !$this->error;
 }