php - Formvalidator check even if value is empty -


thanks forum came across below form validator time work fine. however, have 1 problem.

when submitting form empty textarea instance return empty field error. however, value not mandatory need correct somehow.

<?php /** * pork formvalidator. validates fields regexes , can sanatize them.         uses php       filter_var built-in functions , regexes  * @package pork */   /** * pork.formvalidator * validates arrays or properties setting simple arrays *  * @package pork * @author schizoduckie * @copyright schizoduckie 2009 * @version 1.0 * @access public */ class formvalidator { public static $regexes = array(         'date' => "^[0-9]{4}[-/][0-9]{1,2}[-/][0-9]{1,2}\$",         'datetime' => "20\d{2}(-|\/)((0[1-9])|(1[0-2]))(-|\/)((0[1-9])|([1-2][0-9])|(3[0-1]))(t|\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])",         'amount' => "^[-]?[0-9]+\$",         'number' => "^[-]?[0-9,]+\$",         'alfanum' => "^[0-9a-za-z ,.-_\\s\?\!]+\$",         'not_empty' => "[a-z0-9a-z]+",         'words' => "^[a-za-z]+[a-za-z \\s]*\$",         'phone' => "^[0-9]{10,11}\$",         'zipcode' => "^[1-9][0-9]{3}[a-za-z]{2}\$",         'plate' => "^([0-9a-za-z]{2}[-]){2}[0-9a-za-z]{2}\$",         'price' => "^[0-9.,]*(([.,][-])|([.,][0-9]{2}))?\$",         '2digitopt' => "^\d+(\,\d{2})?\$",         '2digitforce' => "^\d+\,\d\d\$",         'anything' => "^[\d\d]{1,}\$",         'username' => "^[\w]{3,32}\$" );  private $validations, $sanatations, $mandatories, $equal, $errors, $corrects, $fields;   public function __construct($validations=array(), $mandatories = array(), $sanatations = array(), $equal=array()) { $this->validations = $validations; $this->sanatations = $sanatations; $this->mandatories = $mandatories; $this->equal = $equal; $this->errors = array(); $this->corrects = array(); }  /** * validates array of items (if needed) , returns true or false * * jp modofied function checks fields if not submitted. * example original code did not check mandatory field if not submitted. * types of non mandatory fields not checked. */ public function validate($items) { $this->fields = $items; $havefailures = false;  //check mandatories foreach($this->mandatories $key=>$val) {     if(!array_key_exists($val,$items))     {         $havefailures = true;         $this->adderror($val);     } }  //check equal fields foreach($this->equal $key=>$val) {     //check equals field exists     if(!array_key_exists($key,$items))     {         $havefailures = true;         $this->adderror($val);     }      //check field it's supposed equal exists     if(!array_key_exists($val,$items))     {         $havefailures = true;         $this->adderror($val);     }      //check 2 fields equal     if($items[$key] != $items[$val])     {         $havefailures = true;         $this->adderror($key);     } }  foreach($this->validations $key=>$val) {         //an empty value or 1 not in list of validations or 1 not in our list of mandatories         if(!array_key_exists($key,$items))          {                 $this->adderror($key, $val);                 continue;         }          $result = self::validateitem($items[$key], $val);          if($result === false) {                 $havefailures = true;                 $this->adderror($key, $val);         }         else         {                 $this->corrects[] = $key;         } }  return(!$havefailures); }  /* jp * returns json encoded array containing names of fields errors , without.  */ public function getjson() {  $errors = array();  $correct = array();  if(!empty($this->errors)) {                 foreach($this->errors $key=>$val) { $errors[$key] = $val; }             }  if(!empty($this->corrects)) {     foreach($this->corrects $key=>$val) { $correct[$key] = $val; }                 }  $output = array('errors' => $errors, 'correct' => $correct);  return json_encode($output); }    /**  *  * sanatizes array of items according $this->sanatations  * sanatations standard of type string, can specified.  * ease of use, syntax accepted:  * $sanatations = array('fieldname', 'otherfieldname'=>'float'); */ public function sanatize($items) { foreach($items $key=>$val) {         if(array_search($key, $this->sanatations) === false && !array_key_exists($key, $this->sanatations)) continue;         $items[$key] = self::sanatizeitem($val, $this->validations[$key]); } return($items); }   /**  *  * adds error errors array.  */  private function adderror($field, $type='string') { $this->errors[$field] = $type; }  /**  *  * sanatize single var according $type.  * allows static calling allow simple sanatization  */ public static function sanatizeitem($var, $type) { $flags = null; switch($type) {         case 'url':                 $filter = filter_sanitize_url;         break;         case 'int':                 $filter = filter_sanitize_number_int;         break;         case 'float':                 $filter = filter_sanitize_number_float;                 $flags = filter_flag_allow_fraction | filter_flag_allow_thousand;         break;         case 'email':                 $var = substr($var, 0, 254);                 $filter = filter_sanitize_email;         break;         case 'string':         default:                 $filter = filter_sanitize_string;                 $flags = filter_flag_no_encode_quotes;         break;  } $output = filter_var($var, $filter, $flags);             return($output); }  /**   *  * validates single var according $type.  * allows static calling allow simple validation.  *  */ public static function validateitem($var, $type) { if(array_key_exists($type, self::$regexes)) {         $returnval =  filter_var($var, filter_validate_regexp, array("options"=> array("regexp"=>'!'.self::$regexes[$type].'!i'))) !== false;         return($returnval); } $filter = false; switch($type) {         case 'email':                 $var = substr($var, 0, 254);                 $filter = filter_validate_email;                 break;         case 'int':                 $filter = filter_validate_int;         break;         case 'boolean':                 $filter = filter_validate_boolean;         break;         case 'ip':                 $filter = filter_validate_ip;         break;         case 'url':                 $filter = filter_validate_url;         break; } return ($filter === false) ? false : filter_var($var, $filter) !== false ? true :     false; }            } ?> 

so understand need come a way validate empty string above code throw error.

$validations = array(     'id' => 'number', //value in _post['id'] = '11'     'time' => 'datetime', //value in _post['time'] = '2016-03-17t11:05:01'     'description' => 'anything'); //value in _post['decription']  = ''   $required = array('id', 'time');  $validator = new formvalidator($validations, $required);  $validator->validate($_post); print_r $validator->getjson(); 

you should either make field required , add this

// validation     if (empty($variable)) {         echo 'such&such required<br />';         $ok = false;     } 

or make variable ="" or null;


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -