Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Here Here you can see that my validator can already deal with regular variable inputs or non-entity inputs (certain forms for example).

Here you can see that my validator can already deal with regular variable inputs or non-entity inputs (certain forms for example).

Here you can see that my validator can already deal with regular variable inputs or non-entity inputs (certain forms for example).

edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
added 1 character in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
class Validator
{
 privateprotected $rules = [];
 protected $errors = [];
 public function getErrors($rulename = null)
 {
 if ($rulename) {
 return isset($this->errors[$rulename]) ? $this->errors[$rulename] : [];
 }
 return $this->errors;
 }
 public function setRule($name, Array $rules, $required = true)
 {
 $this->rules[$name]['rules'] = $rules;
 switch (true) {
 case ($required === true):
 $this->rules[$name]['required'] = 'Must not be null or empty.';
 break;
 case ($required === false):
 $this->rules[$name]['required'] = false;
 break;
 default:
 $this->rules[$name]['required'] = $required;
 break;
 }
 }
 public function validate($rulename, $input)
 {
 if ($this->rules[$rulename]['required']) {
 if (empty($input)) {
 $this->errors[$rulename][] = $this->rules[$rulename]['required'];
 }
 foreach ($this->rules[$rulename]['rules'] as $rule) {
 if (!$rule->check($input)) {
 $this->errors[$rulename][] = $rule->getError();
 }
 }
 } elseif (!empty($input)) {
 foreach ($this->rules[$rulename]['rules'] as $rule) {
 if (!$rule->check($input)) {
 $this->errors[$rulename][] = $rule->getError();
 }
 }
 }
 return isset($this->errors[$rulename]);
 }
}
class Validator
{
 private $rules = [];
 protected $errors = [];
 public function getErrors($rulename = null)
 {
 if ($rulename) {
 return isset($this->errors[$rulename]) ? $this->errors[$rulename] : [];
 }
 return $this->errors;
 }
 public function setRule($name, Array $rules, $required = true)
 {
 $this->rules[$name]['rules'] = $rules;
 switch (true) {
 case ($required === true):
 $this->rules[$name]['required'] = 'Must not be null or empty.';
 break;
 case ($required === false):
 $this->rules[$name]['required'] = false;
 break;
 default:
 $this->rules[$name]['required'] = $required;
 break;
 }
 }
 public function validate($rulename, $input)
 {
 if ($this->rules[$rulename]['required']) {
 if (empty($input)) {
 $this->errors[$rulename][] = $this->rules[$rulename]['required'];
 }
 foreach ($this->rules[$rulename]['rules'] as $rule) {
 if (!$rule->check($input)) {
 $this->errors[$rulename][] = $rule->getError();
 }
 }
 } elseif (!empty($input)) {
 foreach ($this->rules[$rulename]['rules'] as $rule) {
 if (!$rule->check($input)) {
 $this->errors[$rulename][] = $rule->getError();
 }
 }
 }
 return isset($this->errors[$rulename]);
 }
}
class Validator
{
 protected $rules = [];
 protected $errors = [];
 public function getErrors($rulename = null)
 {
 if ($rulename) {
 return isset($this->errors[$rulename]) ? $this->errors[$rulename] : [];
 }
 return $this->errors;
 }
 public function setRule($name, Array $rules, $required = true)
 {
 $this->rules[$name]['rules'] = $rules;
 switch (true) {
 case ($required === true):
 $this->rules[$name]['required'] = 'Must not be null or empty.';
 break;
 case ($required === false):
 $this->rules[$name]['required'] = false;
 break;
 default:
 $this->rules[$name]['required'] = $required;
 break;
 }
 }
 public function validate($rulename, $input)
 {
 if ($this->rules[$rulename]['required']) {
 if (empty($input)) {
 $this->errors[$rulename][] = $this->rules[$rulename]['required'];
 }
 foreach ($this->rules[$rulename]['rules'] as $rule) {
 if (!$rule->check($input)) {
 $this->errors[$rulename][] = $rule->getError();
 }
 }
 } elseif (!empty($input)) {
 foreach ($this->rules[$rulename]['rules'] as $rule) {
 if (!$rule->check($input)) {
 $this->errors[$rulename][] = $rule->getError();
 }
 }
 }
 return isset($this->errors[$rulename]);
 }
}
deleted 2 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
deleted 89 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
deleted 15 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
added 50 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
edited tags
Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
added 26 characters in body
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
Source Link
Kid Diamond
  • 2.6k
  • 17
  • 35
Loading
lang-php

AltStyle によって変換されたページ (->オリジナル) /