/_examples/mvc2/application/lib/validation.class.php

https://bitbucket.org/tumivn/phpexamples · PHP · 927 lines · 520 code · 103 blank · 304 comment · 53 complexity · d71f6d36c912de504ed38c39f24fe9b3 MD5 · raw file

  1. <?php
  2. /**
  3. * Generic validation class for validation from most sources
  4. *
  5. * @copyright 2013-03-29
  6. * @link http://phpro.org
  7. * @author Kevin Waterson
  8. * @version: $ID$
  9. *
  10. */
  11. namespace sevenkevins;
  12. class validation
  13. {
  14. /**
  15. * @var $errors The array of errors
  16. * @access public
  17. */
  18. public $errors = array();
  19. /**
  20. * @var $validators The array of validators
  21. * @access public
  22. */
  23. public $validators = array();
  24. /**
  25. *
  26. * @var $sanitized The array of sanitized values
  27. * @access public
  28. */
  29. public $sanitized = array();
  30. /**
  31. *
  32. * Settor
  33. *
  34. * @access public
  35. * @param string $name
  36. * @param mixed $value
  37. *
  38. */
  39. public function __set( $name, $value )
  40. {
  41. switch( $name )
  42. {
  43. case 'source':
  44. if( !is_array( $value ) )
  45. {
  46. throw new \Exception( 'Source must be an array' );
  47. }
  48. $this->source = $value;
  49. break;
  50. default:
  51. $this->name = $value;
  52. }
  53. }
  54. /**
  55. *
  56. * Getter
  57. *
  58. * @access public
  59. * @param string $name
  60. * @return string
  61. *
  62. */
  63. public function __get( $name )
  64. {
  65. return $this->$name;
  66. }
  67. /**
  68. * Add a rule for a validator
  69. *
  70. * @access public
  71. * @param array $validator [name, type, required, [min], [max] ]
  72. * @return object Instance of self to allow chaining
  73. *
  74. */
  75. public function addValidator( $validator )
  76. {
  77. /*** set the validator name if it does not exist ***/
  78. if( !isset( $this->validators[$validator['name']] ) )
  79. {
  80. $this->validators[$validator['name']] = array();
  81. }
  82. $val = array();
  83. foreach( $validator as $key=>$value )
  84. {
  85. $val[$key] = $value;
  86. }
  87. $this->validators[$validator['name']][] = $val;
  88. return $this;
  89. }
  90. /**
  91. *
  92. * Run the validations
  93. *
  94. */
  95. public function run()
  96. {
  97. // loop over the validators
  98. foreach( $this->validators as $key=>$val )
  99. {
  100. // each validator may contain multiple rules
  101. foreach( $val as $key=>$options )
  102. {
  103. // check if the field is required
  104. $this->checkRequired( $options );
  105. // run the validation
  106. switch( $options['type'] )
  107. {
  108. case 'string':
  109. $this->validateString( $options );
  110. break;
  111. case 'length':
  112. $this->validateStringLength( $options );
  113. break;
  114. case 'numeric':
  115. $this->validateNumeric( $options );
  116. break;
  117. case 'regex':
  118. $this->validateRegex( $options );
  119. break;
  120. case 'float':
  121. $this->validateFloat( $options );
  122. break;
  123. case 'date':
  124. $this->validateDate( $options );
  125. break;
  126. case 'url':
  127. $this->validateUrl( $options );
  128. break;
  129. case 'email':
  130. $this->validateEmail( $options );
  131. break;
  132. case 'injection':
  133. $this->validateEmailInjection( $options );
  134. break;
  135. case 'ipv4':
  136. $this->validateIpv4( $options );
  137. break;
  138. case 'ipv6':
  139. $this->validateIpv6( $options );
  140. break;
  141. case 'callback':
  142. $this->validateCallback( $options );
  143. break;
  144. case 'compare':
  145. $this->validateCompare( $options );
  146. break;
  147. case 'checkbox':
  148. $this->validateCheckbox( $options );
  149. break;
  150. case 'multiple':
  151. $this->validateMultiple( $options );
  152. break;
  153. case 'file':
  154. $this->validateFile( $options );
  155. break;
  156. case 'radio':
  157. $this->validateRadio( $options );
  158. break;
  159. case 'select':
  160. $this->validateSelect( $options );
  161. break;
  162. default:
  163. throw new \Exception( "Invalid Type" );
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Check if a field is required
  170. *
  171. * @access private
  172. * @param array bool
  173. *
  174. */
  175. private function checkRequired( $options )
  176. {
  177. $message = $this->parseCamelCase( $options['name'] ) . ' is a required field';
  178. $message = isset( $options['message'] ) ? $options['message'] : $message;
  179. if( isset( $options['required'] ) && $options['required'] === true )
  180. {
  181. if( !isset( $this->source[$options['name']] ) || $this->source[$options['name']] == '' )
  182. {
  183. $this->errors[$options['name']] = $message;
  184. }
  185. }
  186. }
  187. /**
  188. *
  189. * Validate a string
  190. *
  191. * @access private
  192. * @param array $options
  193. * @return bool
  194. *
  195. */
  196. private function validateString( $options )
  197. {
  198. // apply trim if set
  199. $this->vTrim( $options );
  200. if( strlen( $options['required'] == false && $this->source[$options['name']] ) == 0 )
  201. {
  202. $min = 0;
  203. }
  204. if( strlen( $options['required'] == false && $this->source[$options['name']] ) > 0 )
  205. {
  206. $min = $options['min'];
  207. }
  208. $message = $this->parseCamelCase( $options['name'] ) . ' length is Invalid';
  209. $message = isset( $options['message'] ) ? $options['message'] : $message;
  210. $name = $options['name'];
  211. if( !is_string( $this->source[$name] ) )
  212. {
  213. $this->errors[$name] = $message;
  214. $this->sanitized[$name] = '';
  215. }
  216. elseif( strlen( $this->source[$options['name']] ) > $options['max'] || strlen( $this->source[$options['name']] ) < $min )
  217. {
  218. if( $options['min'] == $options['max'] )
  219. {
  220. $message = $this->parseCamelCase( $options['name'] ). ' must be exactly ' . $options['min'] . ' characters.';
  221. }
  222. else
  223. {
  224. $message = $this->parseCamelCase( $options['name'] ). ' must be between ' . $options['min'] . ' and ' . $options['max'] . ' characters.';
  225. }
  226. $this->errors[$options['name']] = $message;
  227. $this->sanitized[$options['name']] = '';
  228. }
  229. else
  230. {
  231. $this->sanitized[$name] = filter_var( $this->source[$name], FILTER_SANITIZE_STRING);
  232. }
  233. }
  234. /**
  235. *
  236. * Check the length of a string
  237. *
  238. * @access private
  239. * @param $options The array of options
  240. * @return bool
  241. *
  242. */
  243. private function validateStringLength( $options )
  244. {
  245. // apply trim if set
  246. $this->vTrim( $options );
  247. if( $options['min'] == $options['max'] )
  248. {
  249. $message = $this->parseCamelCase( $options['name'] ). ' must be exactly ' . $options['min'] . ' characters.';
  250. }
  251. else
  252. {
  253. $message = $this->parseCamelCase( $options['name'] ). ' must be between ' . $options['min'] . ' and ' . $values['max'] . ' characters long';
  254. }
  255. $message = isset( $options['message'] ) ? $options['message'] : $message;
  256. if( strlen( $this->source[$options['name']] ) > $options['max'] || strlen( $this->source[$options['name']] ) < $options['min'] )
  257. {
  258. $this->errors[$options['name']] = $message;
  259. $this->sanitized[$options['name']] = '';
  260. }
  261. else
  262. {
  263. $this->sanitized[$options['name']] = $this->source[$options['name']];
  264. }
  265. }
  266. /**
  267. *
  268. * Validate by Regular Expression
  269. *
  270. * @access private
  271. * @param array $options
  272. *
  273. */
  274. public function validateRegex( $options )
  275. {
  276. // apply trim if set
  277. $this->vTrim( $options );
  278. $default_message = $this->parseCamelCase( $options['name'] ) . ' does not match the required pattern';
  279. $message = isset( $options['message'] ) ? $options['message'] : $default_message;
  280. if( !preg_match( "'".$options['pattern']."'", $this->source[$options['name']] ) )
  281. {
  282. $this->errors[$options['name']] = $message;
  283. $this->sanitized[$options['name']] = '';
  284. }
  285. else
  286. {
  287. $this->sanitized[$options['name']] = $this->source[$options['name']];
  288. }
  289. }
  290. /**
  291. *
  292. * Validate a number is numeric
  293. *
  294. * @access private
  295. * @param array $options
  296. */
  297. private function validateNumeric( $options )
  298. {
  299. // apply trim if set
  300. $this->vTrim( $options );
  301. $default_message = $this->parseCamelCase( $options['name'] ) . ' must be a number';
  302. $message = isset( $options['message'] ) ? $options['message'] : $default_message;
  303. if( filter_var( $this->source[$options['name']], FILTER_VALIDATE_INT ) === false )
  304. {
  305. $this->errors[$options['name']] = $message;
  306. $this->sanitized[$options['name']] = '';
  307. }
  308. else
  309. {
  310. $this->sanitized[$options['name']] = filter_var( $this->source[$options['name']], FILTER_VALIDATE_INT );
  311. }
  312. }
  313. /**
  314. *
  315. * Validate an email address
  316. *
  317. * @access private
  318. * @param array $options
  319. *
  320. */
  321. private function validateEmail( $options )
  322. {
  323. // apply trim if set
  324. $this->vTrim( $options );
  325. $default_message = $this->parseCamelCase( $options['name'] ) . ' is not a valid email address';
  326. $message = isset( $options['message'] ) ? $options['message'] : $default_message;
  327. if(filter_var( $this->source[$options['name']], FILTER_VALIDATE_EMAIL ) === FALSE )
  328. {
  329. $this->errors[$options['name']] = $message;
  330. $this->sanitized[$options['name']] = '';
  331. }
  332. else
  333. {
  334. $this->sanitized[$options['name']] = filter_var( $this->source[$options['name']], FILTER_SANITIZE_EMAIL);
  335. }
  336. }
  337. /**
  338. * Check an email for email injection characters
  339. *
  340. * @access private
  341. * @param $options
  342. *
  343. */
  344. private function validateEmailInjection( $options )
  345. {
  346. $default_message = $this->parseCamelCase( $options['name'] ) . ' contains injection characters';
  347. $message = isset( $options['message'] ) ? $options['message'] : $default_message;
  348. if ( preg_match( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , $this->source[$options['name']] ) )
  349. {
  350. $this->errors[$options['name']] = $message;
  351. $this->sanitized[$options['name']] = '';
  352. }
  353. else
  354. {
  355. $this->sanitized[$options['name']] = filter_var( $this->source[$options['name']], FILTER_SANITIZE_EMAIL);
  356. }
  357. }
  358. /**
  359. *
  360. * Validate a value is a floating point number
  361. *
  362. * @access private
  363. * @param array $options
  364. *
  365. */
  366. private function validateFloat( $options )
  367. {
  368. // apply trim if set
  369. $this->vTrim( $options );
  370. $message = $this->parseCamelCase( $options['name'] ) . ' is not a valid floating point number';
  371. $message = isset( $options['message'] ) ? $options['message'] : $message;
  372. if(filter_var( $this->source[$options['name']], FILTER_VALIDATE_FLOAT ) === false )
  373. {
  374. $this->errors[$options['name']] = $message;
  375. $this->sanitized[$options['name']] = '';
  376. }
  377. else
  378. {
  379. // $this->source[$options['name']] = 123.45;
  380. $this->sanitized[$options['name']] = filter_var( $this->source[$options['name']], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
  381. }
  382. }
  383. /**
  384. *
  385. * Parse CamelCase or camel_case to Camel Case
  386. *
  387. * @access private
  388. * @param string $string
  389. * @return string
  390. *
  391. */
  392. private function parseCamelCase( $string )
  393. {
  394. $cc = preg_replace('/(?<=[a-z])(?=[A-Z])/',' ',$string);
  395. $cc = ucwords( str_replace( '_', ' ', $cc ) );
  396. return $cc;
  397. }
  398. /**
  399. *
  400. * Validate a URL
  401. *
  402. * @access private
  403. * @param array $options
  404. *
  405. */
  406. private function validateUrl( $options )
  407. {
  408. // apply trim if set
  409. $this->vTrim( $options );
  410. $message = $this->parseCamelCase( $options['name'] ) . ' is not a valid URL';
  411. $message = isset( $options['message'] ) ? $options['message'] : $message;
  412. if(filter_var( $this->source[$options['name']], FILTER_VALIDATE_URL) === FALSE )
  413. {
  414. $this->errors[$options['name']] = $message;
  415. $this->sanitized[$options['name']] = '';
  416. }
  417. else
  418. {
  419. $this->sanitized[$options['name']] = filter_var( $this->source[$options['name']], FILTER_SANITIZE_URL);
  420. }
  421. }
  422. /**
  423. * Validate a date
  424. *
  425. * @access private
  426. * @param array options
  427. *
  428. */
  429. private function validateDate( $options )
  430. {
  431. // apply trim if set
  432. $this->vTrim( $options );
  433. $message = $this->parseCamelCase( $options['name'] ) . ' is not a valid date';
  434. $message = isset( $options['message'] ) ? $options['message'] : $message;
  435. switch( $options['format'] )
  436. {
  437. case 'YYYY/MM/DD':
  438. case 'YYYY-MM-DD':
  439. list( $y, $m, $d ) = preg_split( '/[-\.\/ ]/', $this->source[$options['name']] );
  440. break;
  441. case 'YYYY/DD/MM':
  442. case 'YYYY-DD-MM':
  443. list( $y, $d, $m ) = preg_split( '/[-\.\/ ]/', $this->source[$options['name']] );
  444. break;
  445. case 'DD-MM-YYYY':
  446. case 'DD/MM/YYYY':
  447. list( $d, $m, $y ) = preg_split( '/[-\.\/ ]/', $this->source[$options['name']] );
  448. break;
  449. case 'MM-DD-YYYY':
  450. case 'MM/DD/YYYY':
  451. list( $m, $d, $y ) = preg_split( '/[-\.\/ ]/', $this->source[$options['name']] );
  452. break;
  453. case 'YYYYMMDD':
  454. $y = substr( $this->source[$options['name']], 0, 4 );
  455. $m = substr( $this->source[$options['name']], 4, 2 );
  456. $d = substr( $this->source[$options['name']], 6, 2 );
  457. break;
  458. case 'YYYYDDMM':
  459. $y = substr( $this->source[$options['name']], 0, 4 );
  460. $d = substr( $this->source[$options['name']], 4, 2 );
  461. $m = substr( $this->source[$options['name']], 6, 2 );
  462. break;
  463. default:
  464. throw new \Exception( "Invalid Date Format" );
  465. }
  466. if( checkdate( $m, $d, $y ) == false )
  467. {
  468. $this->errors[$options['name']] = $message;
  469. $this->sanitized[$options['name']] = '';
  470. }
  471. else
  472. {
  473. $this->sanitized[$options['name']] = $this->source[$options['name']];
  474. }
  475. }
  476. /**
  477. *
  478. * Validate an ipv4 IP address
  479. *
  480. * @access private
  481. * @param array $options
  482. *
  483. */
  484. private function validateIpv4( $options )
  485. {
  486. // apply trim if set
  487. $this->vTrim( $options );
  488. $message = $this->parseCamelCase( $options['name'] ) . ' is not a valid ipv4 address';
  489. $message = isset( $options['message'] ) ? $options['message'] : $message;
  490. if( filter_var( $this->source[$options['name']], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) === FALSE)
  491. {
  492. $this->errors[$options['name']] = $message;
  493. $this->sanitized[$options['name']] = '';
  494. }
  495. else
  496. {
  497. $this->sanitized[$options['name']] = $this->source[$options['name']];
  498. }
  499. }
  500. /**
  501. *
  502. * Validate an ipv6 IP address
  503. *
  504. * @access private
  505. * @param array $options
  506. *
  507. */
  508. private function validateIpv6( $options )
  509. {
  510. // apply trim if set
  511. $this->vTrim( $options );
  512. $message = $this->parseCamelCase( $options['name'] ) . ' is not a valid ipv6 address';
  513. $message = isset( $options['message'] ) ? $options['message'] : $message;
  514. if( filter_var( $this->source[$options['name']], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE )
  515. {
  516. $this->errors[$options['name']] = $message;
  517. $this->sanitized[$options['name']] = '';
  518. }
  519. else
  520. {
  521. $this->sanitized[$options['name']] = $this->source[$options['name']];
  522. }
  523. }
  524. /**
  525. *
  526. * Custom or external validator
  527. *
  528. * @access private
  529. * @param array $options
  530. *
  531. */
  532. public function validateCallback( $options )
  533. {
  534. // apply trim if set
  535. $this->vTrim( $options );
  536. $message = $this->parseCamelCase( $options['name'] ) . ' is invalid';
  537. $message = isset( $options['message'] ) ? $options['message'] : $message;
  538. if( isset( $options['class'] ) )
  539. {
  540. $class = $options['class'];
  541. $func = $options['function'];
  542. $obj = new $class;
  543. // the callback function MUST return bool
  544. if( $obj->$func( $this->source[$options['name']] ) == true )
  545. {
  546. $this->errors[$options['name']] = $message;
  547. }
  548. }
  549. else
  550. {
  551. $func = $options['function'];
  552. if( $func( $this->source[$options['name']] ) == true )
  553. {
  554. $this->errors[$options['name']] = $message;
  555. }
  556. }
  557. }
  558. /**
  559. *
  560. * Compare two values
  561. *
  562. * @access public
  563. * @param array $options
  564. * @return bool
  565. *
  566. */
  567. public function validateCompare( $options )
  568. {
  569. $message = $this->parseCamelCase( $options['name'] ).' and '.$this->parseCamelCase( $options['compare_to'] ) . ' do not match';
  570. $message = isset( $options['message'] ) ? $options['message'] : $message;
  571. if( $this->source[$options['name']] !== $this->source[$options['compare_to']] )
  572. {
  573. $this->errors[$options['name']] = $message;
  574. $this->sanitized[$options['name']] = '';
  575. $this->sanitized[$options['compare_to']] = '';
  576. }
  577. else
  578. {
  579. $this->sanitized[$options['name']] = $this->source[$options['name']];
  580. }
  581. }
  582. /*
  583. * Validate that a checkbox is checked
  584. *
  585. * @access public
  586. * @param array $options
  587. * @return bool
  588. *
  589. */
  590. public function validateCheckbox( $options )
  591. {
  592. $message = $this->parseCamelCase( $options['name'] ).' is a mandatory field';
  593. $message = isset( $options['message'] ) ? $options['message'] : $message;
  594. if( $this->source[$options['name']] !== 'on' )
  595. {
  596. $this->errors[$options['name']] = $message;
  597. $this->sanitized[$options['name']] = '';
  598. }
  599. else
  600. {
  601. $this->sanitized[$options['name']] = 'checked';
  602. }
  603. }
  604. /**
  605. *
  606. * Validate a field with multiple values
  607. *
  608. * @access public
  609. * @param array $options
  610. * @return bool
  611. *
  612. */
  613. public function validateMultiple( $options )
  614. {
  615. switch( $options['val_type'] )
  616. {
  617. case 'numeric':
  618. foreach( $this->source[$options['name']] as $opt )
  619. {
  620. if( !is_numeric( $opt ) )
  621. {
  622. $message = $this->parseCamelCase( $options['name'] ).' values must be numeric';
  623. $this->errors->$options['name'] = $message;
  624. break;
  625. }
  626. else
  627. {
  628. }
  629. }
  630. $this->sanitized[$options['name']] = $this->source[$options['name']];
  631. }
  632. }
  633. /**
  634. *
  635. * Validate if at least one of many fields is set
  636. *
  637. * @access public
  638. * @param array $options
  639. * @return bool
  640. *
  641. */
  642. public function oneOfMany( $options )
  643. {
  644. // array of fields
  645. foreach( $options['fields'] as $field )
  646. {
  647. if( isset( $this->source[$field] ) && $this->source[$field] != '' )
  648. {
  649. return true;
  650. }
  651. }
  652. // is any of the fields present in source?
  653. }
  654. /**
  655. *
  656. * Validate a file upload
  657. *
  658. * @access public
  659. * @param array $options
  660. * @return bool
  661. *
  662. */
  663. public function validateFile( $options )
  664. {
  665. // apply trim if set
  666. $this->vTrim( $options );
  667. $name = $options['name'];
  668. $max_size = $options['max_size'];
  669. $allowed_types = $options['allowed_types'];
  670. $ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
  671. $upload_max = $ini_max * 1024;
  672. $tmp = $_FILES[$name]["tmp_name"];
  673. $filename = $_FILES[$name]["name"];
  674. $file_extension = pathinfo( $filename, PATHINFO_EXTENSION );
  675. if( !in_array( $file_extension, $allowed_types ) )
  676. {
  677. $this->errors[$options['name']] = "File '$file_extension' type not supported";
  678. }
  679. // check a file has been uploaded
  680. elseif(!is_uploaded_file( $_FILES[$name]['tmp_name'] ) )
  681. {
  682. $this->errors[$options['name']] = 'No file uploaed';
  683. }
  684. elseif( $_FILES['userfile']['size'] > $max_size )
  685. {
  686. // if the file sizse is greater than the max size
  687. $this->errors[$options['name']] = 'File size exceeds upload limit';
  688. }
  689. elseif( $_FILES['userfile']['size'] > $upload_max )
  690. {
  691. $this->errors[$options['name']] = 'File exceeds system upload max size';
  692. }
  693. $this->sanitized[$options['name']] = $this->source[$options['name']];
  694. }
  695. /*
  696. *
  697. * Check if this validation has errors
  698. *
  699. * @access public
  700. * @return bool
  701. *
  702. */
  703. public function isValid()
  704. {
  705. return sizeof( $this->errors ) == 0;
  706. }
  707. /**
  708. *
  709. * If a trim option is set, remove trailing and leading whitespace
  710. *
  711. * @access private
  712. * @param array $options
  713. * @return array The options array
  714. *
  715. */
  716. private function vTrim( $options )
  717. {
  718. // if the trim option is set, strip leading and trailing whitespace
  719. if( isset( $options['trim'] ) && $options['trim'] == true )
  720. {
  721. $this->source[$options['name']] = trim( $this->source[$options['name']] );
  722. }
  723. return $options;
  724. }
  725. /**
  726. *
  727. * Define the error message
  728. *
  729. * @access private
  730. * @param array $options
  731. *
  732. */
  733. private function errorMessage( $options )
  734. {
  735. }
  736. /**
  737. *
  738. * Validate radio buttons
  739. * example Usage
  740. * $this->addValidator( array( 'name'=>'payment_method', 'type'=>'radio', 'required'=>true, 'data_type'=>'string', 'values'=>array( 'cc', 'paypal'), 'message'=>'Payment method is not a supported type' ) );
  741. *
  742. * @access private
  743. * @param array $options
  744. *
  745. */
  746. public function validateRadio( $options )
  747. {
  748. $message = $this->parseCamelCase( $options['name'] ) . ' is an invalid selection';
  749. $message = isset( $options['message'] ) ? $options['message'] : $message;
  750. $values = $options['values'];
  751. if( sizeof( $values ) == 0 )
  752. {
  753. $this->errors[$options['name']] = $message;
  754. $this->sanitized[$options['name']] = '';
  755. }
  756. elseif( !isset( $this->source[$options['name']] ) || !in_array( $this->source[$options['name']], $values ) )
  757. {
  758. $this->errors[$options['name']] = $message;
  759. $this->sanitized[$options['name']] = '';
  760. }
  761. else
  762. {
  763. $this->sanitized[$options['name']] = $this->source[$options['name']];
  764. }
  765. }
  766. /**
  767. *
  768. * Validate radio buttons
  769. * example Usage
  770. * $this->addValidator( array( 'name'=>'payment_method', 'type'=>'select', 'required'=>true, 'data_type'=>'string', 'values'=>array( 'cc', 'paypal'), 'message'=>'Payment method is not a supported type' ) );
  771. *
  772. * @access private
  773. * @param array $options
  774. *
  775. */
  776. public function validateSelect( $options )
  777. {
  778. $message = $this->parseCamelCase( $options['name'] ) . ' is an invalid selection';
  779. $message = isset( $options['message'] ) ? $options['message'] : $message;
  780. $values = $options['values'];
  781. if( sizeof( $values ) == 0 )
  782. {
  783. $this->errors[$options['name']] = $message;
  784. $this->sanitized[$options['name']] = '';
  785. }
  786. elseif( !in_array( $this->source[$options['name']], $values ) )
  787. {
  788. $this->errors[$options['name']] = $message;
  789. $this->sanitized[$options['name']] = '';
  790. }
  791. else
  792. {
  793. $this->sanitized[$options['name']] = $this->source[$options['name']];
  794. }
  795. }
  796. } // end of validation class
  797. /**
  798. Example usage
  799. $array = array( 'one'=>' dingo', 'two'=>' wombat', 'three'=>'stever irwin', 'four'=>'platypus', 'five'=>'koala' );
  800. $val = new validation;
  801. $val->source = $array;
  802. $val->addValidator( array( 'name'=>'one', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>100, 'trim'=>true ) );
  803. $val->addValidator( array( 'name'=>'two', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>100 ) );
  804. $val->addValidator( array( 'name'=>'three', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>100 ) );
  805. $val->addValidator( array( 'name'=>'four', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>100 ) );
  806. $val->addValidator( array( 'name'=>'five', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>100 ) );
  807. $val->run();
  808. if( $val->isValid() )
  809. {
  810. foreach( $val->sanitized as $good )
  811. {
  812. echo "$good\n";
  813. }
  814. }
  815. else
  816. {
  817. foreach( $val->errors as $err )
  818. {
  819. echo "$err\n";
  820. }
  821. }
  822. */
  823. ?>