PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Validation/ValidatorAwareTrait.php

https://github.com/binondord/cakephp
PHP | 128 lines | 30 code | 8 blank | 90 comment | 5 complexity | fd596046f111a4d503eaa6fbe7dfaa67 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.3
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Validation;
  16. use Cake\Event\EventDispatcherInterface;
  17. /**
  18. * A trait that provides methods for building and
  19. * interacting with Validators.
  20. *
  21. * This trait is useful when building ORM like features where
  22. * the implementing class wants to build and customize a variety
  23. * of validator instances.
  24. *
  25. * This trait expects that classes including it define two constants:
  26. *
  27. * - `DEFAULT_VALIDATOR` - The default validator name.
  28. * - `VALIDATOR_PROVIDER_NAME ` - The provider name the including class is assigned
  29. * in validators.
  30. *
  31. * If the including class also implements events the `Model.buildValidator` event
  32. * will be triggered when validators are created.
  33. */
  34. trait ValidatorAwareTrait
  35. {
  36. /**
  37. * Validator class.
  38. *
  39. * @var string
  40. */
  41. protected $_validatorClass = '\Cake\Validation\Validator';
  42. /**
  43. * A list of validation objects indexed by name
  44. *
  45. * @var array
  46. */
  47. protected $_validators = [];
  48. /**
  49. * Returns the validation rules tagged with $name. It is possible to have
  50. * multiple different named validation sets, this is useful when you need
  51. * to use varying rules when saving from different routines in your system.
  52. *
  53. * There are two different ways of creating and naming validation sets: by
  54. * creating a new method inside your own Table subclass, or by building
  55. * the validator object yourself and storing it using this method.
  56. *
  57. * For example, if you wish to create a validation set called 'forSubscription',
  58. * you will need to create a method in your Table subclass as follows:
  59. *
  60. * ```
  61. * public function validationForSubscription($validator)
  62. * {
  63. * return $validator
  64. * ->add('email', 'valid-email', ['rule' => 'email'])
  65. * ->add('password', 'valid', ['rule' => 'notBlank'])
  66. * ->requirePresence('username');
  67. * }
  68. * ```
  69. *
  70. * Otherwise, you can build the object by yourself and store it in the Table object:
  71. *
  72. * ```
  73. * $validator = new \Cake\Validation\Validator($table);
  74. * $validator
  75. * ->add('email', 'valid-email', ['rule' => 'email'])
  76. * ->add('password', 'valid', ['rule' => 'notBlank'])
  77. * ->allowEmpty('bio');
  78. * $table->validator('forSubscription', $validator);
  79. * ```
  80. *
  81. * You can implement the method in `validationDefault` in your Table subclass
  82. * should you wish to have a validation set that applies in cases where no other
  83. * set is specified.
  84. *
  85. * @param string $name the name of the validation set to return
  86. * @param \Cake\Validation\Validator|null $validator The validator instance to store,
  87. * use null to get a validator.
  88. * @return \Cake\Validation\Validator
  89. */
  90. public function validator($name = null, Validator $validator = null)
  91. {
  92. if ($name === null) {
  93. $name = self::DEFAULT_VALIDATOR;
  94. }
  95. if ($validator === null && isset($this->_validators[$name])) {
  96. return $this->_validators[$name];
  97. }
  98. if ($validator === null) {
  99. $validator = new $this->_validatorClass;
  100. $validator = $this->{'validation' . ucfirst($name)}($validator);
  101. if ($this instanceof EventDispatcherInterface) {
  102. $this->dispatchEvent('Model.buildValidator', compact('validator', 'name'));
  103. }
  104. }
  105. $validator->provider(self::VALIDATOR_PROVIDER_NAME, $this);
  106. return $this->_validators[$name] = $validator;
  107. }
  108. /**
  109. * Returns the default validator object. Subclasses can override this function
  110. * to add a default validation set to the validator object.
  111. *
  112. * @param \Cake\Validation\Validator $validator The validator that can be modified to
  113. * add some rules to it.
  114. * @return \Cake\Validation\Validator
  115. */
  116. public function validationDefault(Validator $validator)
  117. {
  118. return $validator;
  119. }
  120. }