PageRenderTime 1201ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/form/validation/FormValidatorArray.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 96 lines | 42 code | 15 blank | 39 comment | 13 complexity | 32ade3928fe1907bdc8f620453eb6446 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/form/validation/FormValidatorArray.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class FormValidatorArray
  9. * @ingroup form_validation
  10. *
  11. * @brief Form validation check that checks an array of fields.
  12. */
  13. import('form.validation.FormValidator');
  14. class FormValidatorArray extends FormValidator {
  15. /** @var array Array of fields to check */
  16. var $_fields;
  17. /** @var array Array of field names where an error occurred */
  18. var $_errorFields;
  19. /**
  20. * Constructor.
  21. * @param $form Form the associated form
  22. * @param $field string the name of the associated field
  23. * @param $type string the type of check, either "required" or "optional"
  24. * @param $message string the error message for validation failures (i18n key)
  25. * @param $fields array all subfields for each item in the array, i.e. name[][foo]. If empty it is assumed that name[] is a data field
  26. */
  27. function FormValidatorArray(&$form, $field, $type, $message, $fields = array()) {
  28. parent::FormValidator($form, $field, $type, $message);
  29. $this->_fields = $fields;
  30. $this->_errorFields = array();
  31. }
  32. //
  33. // Setters and Getters
  34. //
  35. /**
  36. * Get array of fields where an error occurred.
  37. * @return array
  38. */
  39. function getErrorFields() {
  40. return $this->_errorFields;
  41. }
  42. //
  43. // Public methods
  44. //
  45. /**
  46. * @see FormValidator::isValid()
  47. * Value is valid if it is empty and optional or all field values are set.
  48. * @return boolean
  49. */
  50. function isValid() {
  51. if ($this->getType() == FORM_VALIDATOR_OPTIONAL_VALUE) return true;
  52. $data = $this->getFieldValue();
  53. if (!is_array($data)) return false;
  54. $isValid = true;
  55. foreach ($data as $key => $value) {
  56. if (count($this->_fields) == 0) {
  57. // We expect all fields to contain values.
  58. if (is_null($value) || trim((string)$value) == '') {
  59. $isValid = false;
  60. array_push($this->_errorFields, $this->getField()."[{$key}]");
  61. }
  62. } else {
  63. // In the two-dimensional case we always expect a value array.
  64. if (!is_array($value)) {
  65. $isValid = false;
  66. array_push($this->_errorFields, $this->getField()."[{$key}]");
  67. continue;
  68. }
  69. // Go through all sub-sub-fields and check them explicitly
  70. foreach ($this->_fields as $field) {
  71. if (!isset($value[$field]) || trim((string)$value[$field]) == '') {
  72. $isValid = false;
  73. array_push($this->_errorFields, $this->getField()."[{$key}][{$field}]");
  74. }
  75. }
  76. }
  77. }
  78. return $isValid;
  79. }
  80. }
  81. ?>