PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/libraries/fb-forms/php-form-validator/samples/4-custom-validation/formvalidator.php

https://bitbucket.org/fbertagnin/fbwork4
PHP | 573 lines | 487 code | 58 blank | 28 comment | 79 complexity | 8ff7dabedcc41547ccbf32497694cb30 MD5 | raw file
  1. <?PHP
  2. /*
  3. -------------------------------------------------------------------------
  4. PHP Form Validator (formvalidator.php)
  5. Version 1.1
  6. This program is free software published under the
  7. terms of the GNU Lesser General Public License.
  8. This program is distributed in the hope that it will
  9. be useful - WITHOUT ANY WARRANTY; without even the
  10. implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE.
  12. For updates, please visit:
  13. http://www.html-form-guide.com/php-form/php-form-validation.html
  14. Questions & comments please send to info@html-form-guide.com
  15. -------------------------------------------------------------------------
  16. */
  17. /**
  18. * Carries information about each of the form validations
  19. */
  20. class ValidatorObj
  21. {
  22. var $variable_name;
  23. var $validator_string;
  24. var $error_string;
  25. }
  26. /**
  27. * Base class for custom validation objects
  28. **/
  29. class CustomValidator
  30. {
  31. function DoValidate(&$formars,&$error_hash)
  32. {
  33. return true;
  34. }
  35. }
  36. /** Default error messages*/
  37. define("E_VAL_REQUIRED_VALUE","Please enter the value for %s");
  38. define("E_VAL_MAXLEN_EXCEEDED","Maximum length exceeded for %s.");
  39. define("E_VAL_MINLEN_CHECK_FAILED","Please enter input with length more than %d for %s");
  40. define("E_VAL_ALNUM_CHECK_FAILED","Please provide an alpha-numeric input for %s");
  41. define("E_VAL_ALNUM_S_CHECK_FAILED","Please provide an alpha-numeric input for %s");
  42. define("E_VAL_NUM_CHECK_FAILED","Please provide numeric input for %s");
  43. define("E_VAL_ALPHA_CHECK_FAILED","Please provide alphabetic input for %s");
  44. define("E_VAL_ALPHA_S_CHECK_FAILED","Please provide alphabetic input for %s");
  45. define("E_VAL_EMAIL_CHECK_FAILED","Please provide a valida email address");
  46. define("E_VAL_LESSTHAN_CHECK_FAILED","Enter a value less than %f for %s");
  47. define("E_VAL_GREATERTHAN_CHECK_FAILED","Enter a value greater than %f for %s");
  48. define("E_VAL_REGEXP_CHECK_FAILED","Please provide a valid input for %s");
  49. define("E_VAL_DONTSEL_CHECK_FAILED","Wrong option selected for %s");
  50. define("E_VAL_SELMIN_CHECK_FAILED","Please select minimum %d options for %s");
  51. define("E_VAL_SELONE_CHECK_FAILED","Please select an option for %s");
  52. define("E_VAL_EQELMNT_CHECK_FAILED","Value of %s should be same as that of %s");
  53. define("E_VAL_NEELMNT_CHECK_FAILED","Value of %s should not be same as that of %s");
  54. /**
  55. * FormValidator: The main class that does all the form validations
  56. **/
  57. class FormValidator
  58. {
  59. var $validator_array;
  60. var $error_hash;
  61. var $custom_validators;
  62. function FormValidator()
  63. {
  64. $this->validator_array = array();
  65. $this->error_hash = array();
  66. $this->custom_validators=array();
  67. }
  68. function AddCustomValidator(&$customv)
  69. {
  70. array_push($this->custom_validators,$customv);
  71. }
  72. function addValidation($variable,$validator,$error)
  73. {
  74. $validator_obj = new ValidatorObj();
  75. $validator_obj->variable_name = $variable;
  76. $validator_obj->validator_string = $validator;
  77. $validator_obj->error_string = $error;
  78. array_push($this->validator_array,$validator_obj);
  79. }
  80. function GetErrors()
  81. {
  82. return $this->error_hash;
  83. }
  84. function ValidateForm()
  85. {
  86. $bret = true;
  87. $error_string="";
  88. $error_to_display = "";
  89. if(strcmp($_SERVER['REQUEST_METHOD'],'POST')==0)
  90. {
  91. $form_variables = $_POST;
  92. }
  93. else
  94. {
  95. $form_variables = $_GET;
  96. }
  97. $vcount = count($this->validator_array);
  98. foreach($this->validator_array as $val_obj)
  99. {
  100. if(!$this->ValidateObject($val_obj,$form_variables,$error_string))
  101. {
  102. $bret = false;
  103. $this->error_hash[$val_obj->variable_name] = $error_string;
  104. }
  105. }
  106. if(true == $bret && count($this->custom_validators) > 0)
  107. {
  108. foreach( $this->custom_validators as $custom_val)
  109. {
  110. if(false == $custom_val->DoValidate($form_variables,$this->error_hash))
  111. {
  112. $bret = false;
  113. }
  114. }
  115. }
  116. return $bret;
  117. }
  118. function ValidateObject($validatorobj,$formvariables,&$error_string)
  119. {
  120. $bret = true;
  121. $splitted = explode("=",$validatorobj->validator_string);
  122. $command = $splitted[0];
  123. $command_value = '';
  124. if(isset($splitted[1]) && strlen($splitted[1])>0)
  125. {
  126. $command_value = $splitted[1];
  127. }
  128. $default_error_message="";
  129. $input_value ="";
  130. if(isset($formvariables[$validatorobj->variable_name]))
  131. {
  132. $input_value = $formvariables[$validatorobj->variable_name];
  133. }
  134. $bret = $this->ValidateCommand($command,$command_value,$input_value,
  135. $default_error_message,
  136. $validatorobj->variable_name,
  137. $formvariables);
  138. if(false == $bret)
  139. {
  140. if(isset($validatorobj->error_string) &&
  141. strlen($validatorobj->error_string)>0)
  142. {
  143. $error_string = $validatorobj->error_string;
  144. }
  145. else
  146. {
  147. $error_string = $default_error_message;
  148. }
  149. }//if
  150. return $bret;
  151. }
  152. function validate_req($input_value, &$default_error_message,$variable_name)
  153. {
  154. $bret = true;
  155. if(!isset($input_value) ||
  156. strlen($input_value) <=0)
  157. {
  158. $bret=false;
  159. $default_error_message = sprintf(E_VAL_REQUIRED_VALUE,$variable_name);
  160. }
  161. return $bret;
  162. }
  163. function validate_maxlen($input_value,$max_len,$variable_name,&$default_error_message)
  164. {
  165. $bret = true;
  166. if(isset($input_value) )
  167. {
  168. $input_length = strlen($input_value);
  169. if($input_length > $max_len)
  170. {
  171. $bret=false;
  172. $default_error_message = sprintf(E_VAL_MAXLEN_EXCEEDED,$variable_name);
  173. }
  174. }
  175. return $bret;
  176. }
  177. function validate_minlen($input_value,$min_len,$variable_name,&$default_error_message)
  178. {
  179. $bret = true;
  180. if(isset($input_value) )
  181. {
  182. $input_length = strlen($input_value);
  183. if($input_length < $min_len)
  184. {
  185. $bret=false;
  186. $default_error_message = sprintf(E_VAL_MINLEN_CHECK_FAILED,$min_len,$variable_name);
  187. }
  188. }
  189. return $bret;
  190. }
  191. function test_datatype($input_value,$reg_exp)
  192. {
  193. if(ereg($reg_exp,$input_value))
  194. {
  195. return false;
  196. }
  197. return true;
  198. }
  199. function validate_email($email)
  200. {
  201. return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
  202. }
  203. function validate_for_numeric_input($input_value,&$validation_success)
  204. {
  205. $more_validations=true;
  206. $validation_success = true;
  207. if(strlen($input_value)>0)
  208. {
  209. if(false == is_numeric($input_value))
  210. {
  211. $validation_success = false;
  212. $more_validations=false;
  213. }
  214. }
  215. else
  216. {
  217. $more_validations=false;
  218. }
  219. return $more_validations;
  220. }
  221. function validate_lessthan($command_value,$input_value,
  222. $variable_name,&$default_error_message)
  223. {
  224. $bret = true;
  225. if(false == $this->validate_for_numeric_input($input_value,
  226. $bret))
  227. {
  228. return $bret;
  229. }
  230. if($bret)
  231. {
  232. $lessthan = doubleval($command_value);
  233. $float_inputval = doubleval($input_value);
  234. if($float_inputval >= $lessthan)
  235. {
  236. $default_error_message = sprintf(E_VAL_LESSTHAN_CHECK_FAILED,
  237. $lessthan,
  238. $variable_name);
  239. $bret = false;
  240. }//if
  241. }
  242. return $bret ;
  243. }
  244. function validate_greaterthan($command_value,$input_value,$variable_name,&$default_error_message)
  245. {
  246. $bret = true;
  247. if(false == $this->validate_for_numeric_input($input_value,$bret))
  248. {
  249. return $bret;
  250. }
  251. if($bret)
  252. {
  253. $greaterthan = doubleval($command_value);
  254. $float_inputval = doubleval($input_value);
  255. if($float_inputval <= $greaterthan)
  256. {
  257. $default_error_message = sprintf(E_VAL_GREATERTHAN_CHECK_FAILED,
  258. $greaterthan,
  259. $variable_name);
  260. $bret = false;
  261. }//if
  262. }
  263. return $bret ;
  264. }
  265. function validate_select($input_value,$command_value,&$default_error_message,$variable_name)
  266. {
  267. $bret=false;
  268. if(is_array($input_value))
  269. {
  270. foreach($input_value as $value)
  271. {
  272. if($value == $command_value)
  273. {
  274. $bret=true;
  275. break;
  276. }
  277. }
  278. }
  279. else
  280. {
  281. if($command_value == $input_value)
  282. {
  283. $bret=true;
  284. }
  285. }
  286. if(false == $bret)
  287. {
  288. $default_error_message = sprintf(E_VAL_SHOULD_SEL_CHECK_FAILED,
  289. $command_value,$variable_name);
  290. }
  291. return $bret;
  292. }
  293. function validate_dontselect($input_value,$command_value,&$default_error_message,$variable_name)
  294. {
  295. $bret=true;
  296. if(is_array($input_value))
  297. {
  298. foreach($input_value as $value)
  299. {
  300. if($value == $command_value)
  301. {
  302. $bret=false;
  303. $default_error_message = sprintf(E_VAL_DONTSEL_CHECK_FAILED,$variable_name);
  304. break;
  305. }
  306. }
  307. }
  308. else
  309. {
  310. if($command_value == $input_value)
  311. {
  312. $bret=false;
  313. $default_error_message = sprintf(E_VAL_DONTSEL_CHECK_FAILED,$variable_name);
  314. }
  315. }
  316. return $bret;
  317. }
  318. function ValidateCommand($command,$command_value,$input_value,&$default_error_message,$variable_name,$formvariables)
  319. {
  320. $bret=true;
  321. switch($command)
  322. {
  323. case 'req':
  324. {
  325. $bret = $this->validate_req($input_value, $default_error_message,$variable_name);
  326. break;
  327. }
  328. case 'maxlen':
  329. {
  330. $max_len = intval($command_value);
  331. $bret = $this->validate_maxlen($input_value,$max_len,$variable_name,
  332. $default_error_message);
  333. break;
  334. }
  335. case 'minlen':
  336. {
  337. $min_len = intval($command_value);
  338. $bret = $this->validate_minlen($input_value,$min_len,$variable_name,
  339. $default_error_message);
  340. break;
  341. }
  342. case 'alnum':
  343. {
  344. $bret= $this->test_datatype($input_value,"[^A-Za-z0-9]");
  345. if(false == $bret)
  346. {
  347. $default_error_message = sprintf(E_VAL_ALNUM_CHECK_FAILED,$variable_name);
  348. }
  349. break;
  350. }
  351. case 'alnum_s':
  352. {
  353. $bret= $this->test_datatype($input_value,"[^A-Za-z0-9 ]");
  354. if(false == $bret)
  355. {
  356. $default_error_message = sprintf(E_VAL_ALNUM_S_CHECK_FAILED,$variable_name);
  357. }
  358. break;
  359. }
  360. case 'num':
  361. case 'numeric':
  362. {
  363. $bret= $this->test_datatype($input_value,"[^0-9]");
  364. if(false == $bret)
  365. {
  366. $default_error_message = sprintf(E_VAL_NUM_CHECK_FAILED,$variable_name);
  367. }
  368. break;
  369. }
  370. case 'alpha':
  371. {
  372. $bret= $this->test_datatype($input_value,"[^A-Za-z]");
  373. if(false == $bret)
  374. {
  375. $default_error_message = sprintf(E_VAL_ALPHA_CHECK_FAILED,$variable_name);
  376. }
  377. break;
  378. }
  379. case 'alpha_s':
  380. {
  381. $bret= $this->test_datatype($input_value,"[^A-Za-z ]");
  382. if(false == $bret)
  383. {
  384. $default_error_message = sprintf(E_VAL_ALPHA_S_CHECK_FAILED,$variable_name);
  385. }
  386. break;
  387. }
  388. case 'email':
  389. {
  390. if(isset($input_value) && strlen($input_value)>0)
  391. {
  392. $bret= $this->validate_email($input_value);
  393. if(false == $bret)
  394. {
  395. $default_error_message = E_VAL_EMAIL_CHECK_FAILED;
  396. }
  397. }
  398. break;
  399. }
  400. case "lt":
  401. case "lessthan":
  402. {
  403. $bret = $this->validate_lessthan($command_value,
  404. $input_value,
  405. $variable_name,
  406. $default_error_message);
  407. break;
  408. }
  409. case "gt":
  410. case "greaterthan":
  411. {
  412. $bret = $this->validate_greaterthan($command_value,
  413. $input_value,
  414. $variable_name,
  415. $default_error_message);
  416. break;
  417. }
  418. case "regexp":
  419. {
  420. if(isset($input_value) && strlen($input_value)>0)
  421. {
  422. if(!preg_match("$command_value",$input_value))
  423. {
  424. $bret=false;
  425. $default_error_message = sprintf(E_VAL_REGEXP_CHECK_FAILED,$variable_name);
  426. }
  427. }
  428. break;
  429. }
  430. case "dontselect":
  431. case "dontselectchk":
  432. case "dontselectradio":
  433. {
  434. $bret = $this->validate_dontselect($input_value,
  435. $command_value,
  436. $default_error_message,
  437. $variable_name);
  438. break;
  439. }//case
  440. case "shouldselchk":
  441. case "selectradio":
  442. {
  443. $bret = $this->validate_select($input_value,
  444. $command_value,
  445. $default_error_message,
  446. $variable_name);
  447. break;
  448. }//case
  449. case "selmin":
  450. {
  451. $min_count = intval($command_value);
  452. if(isset($input_value))
  453. {
  454. if($min_count > 1)
  455. {
  456. $bret = (count($input_value) >= $min_count )?true:false;
  457. }
  458. else
  459. {
  460. $bret = true;
  461. }
  462. }
  463. else
  464. {
  465. $bret= false;
  466. $default_error_message = sprintf(E_VAL_SELMIN_CHECK_FAILED,$min_count,$variable_name);
  467. }
  468. break;
  469. }//case
  470. case "selone":
  471. {
  472. if(false == isset($input_value)||
  473. strlen($input_value)<=0)
  474. {
  475. $bret= false;
  476. $default_error_message = sprintf(E_VAL_SELONE_CHECK_FAILED,$variable_name);
  477. }
  478. break;
  479. }
  480. case "eqelmnt":
  481. {
  482. if(isset($formvariables[$command_value]) &&
  483. strcmp($input_value,$formvariables[$command_value])==0 )
  484. {
  485. $bret=true;
  486. }
  487. else
  488. {
  489. $bret= false;
  490. $default_error_message = sprintf(E_VAL_EQELMNT_CHECK_FAILED,$variable_name,$command_value);
  491. }
  492. break;
  493. }
  494. case "neelmnt":
  495. {
  496. if(isset($formvariables[$command_value]) &&
  497. strcmp($input_value,$formvariables[$command_value]) !=0 )
  498. {
  499. $bret=true;
  500. }
  501. else
  502. {
  503. $bret= false;
  504. $default_error_message = sprintf(E_VAL_NEELMNT_CHECK_FAILED,$variable_name,$command_value);
  505. }
  506. break;
  507. }
  508. }//switch
  509. return $bret;
  510. }//validdate command
  511. }
  512. ?>