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); }
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; }
<?php ini_set('error_reporting', E_ALL | E_STRICT); ini_set('display_errors', 1); ini_set('log_errors', 'Off'); error_reporting(E_ALL); require 'config.php'; $request = new A_Http_Request(); $validator = new A_Rule_Set(); // Alnum: $validator->addRule('A_Rule_Alnum', 'alnum', 'Please fill in a valid alnum'); // Alpha: $alpha = new A_Rule_Alpha('alpha', 'alpha is not alpha'); $validator->addRule($alpha, array('alpha'), array('alpha is not alpha')); // Date: $validator->addRule('A_Rule_Date', 'date', 'Please fill in a valid date'); // Digit: $validator->addRule('A_Rule_Digit', 'digit', 'Please fill in a valid digit'); //Email: $validator->addRule('A_Rule_Email', 'email', 'Please fill in a valid email'); // Inarray: $validator->addRule('A_Rule_Inarray', array('cat', 'dog', 'fish'), 'inarray', 'Please pick one out of the 3 options'); // 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');
<?php error_reporting(E_ALL); require 'config.php'; $request = new A_Http_Request(); $validator = new A_Rule_Set(); $alpha = new A_Rule_Alpha('one', 'One is not alpha'); $length = new A_Rule_Length(4, null, 'two', 'Two must be {min} characters long'); $match = new A_Rule_Match('two', 'one', 'Fields do not match'); $numeric = new A_Rule_Numeric('two', 'Two is not numeric'); $validator->addRule($alpha, array('one', 'two'), array('One is not alpha', 'Two is not alpha')); $validator->addRule($match); #$validator->addRule($length); $validator->addRule('A_Rule_Length', 4, null, 'two', 'Two must be {min} characters long'); #$validator->excludeRules(array('one')); #$validator->excludeRules(array('two')); #$validator->includeRules(array('one')); #$validator->includeRules(array('two')); 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> One must be Alpha. Two must be Numeric. The two values must match.<br/>
<?php error_reporting(E_ALL); require 'config.php'; $request = new A_Http_Request(); $validator = new A_Rule_Set(); $alpha = new A_Rule_Alpha('one', 'One is not alpha'); $length = new A_Rule_Length('two', 5, null, 'Two must be 5 characters long'); $match = new A_Rule_Match('one', 'two', 'Fields do not match'); $numeric = new A_Rule_Numeric('two', 'Two is not numeric'); $regexp = new A_Rule_Regexp('/^[A-Za-z0-9]+$/D', 'three', 'Three must match ^[A-Za-z0-9]+$'); $validator->addRule($alpha); $validator->addRule($match); #$validator->addRule($length); $validator->addRule('A_Rule_Length', 'two', 5, null, 'Two must be 5 characters long'); $validator->addRule($regexp); if ($validator->isValid($request)) { $errmsg = 'OK'; } else { $errmsg = print_r($validator->getErrorMsg(), 1); } ?> <html> <head> <title>A_Rule_Set and Rules Example</title> </head> <body> <h2>Validator Example</h2> One must be Alpha. Two must be Numeric. The two values must match.<br/> <form action="" method="post"> Status: <span style="color:red"><pre><?php
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; }