Validate.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. */
  11. namespace FormBuilder\UI\Iview;
  12. use FormBuilder\Contract\ValidateInterface;
  13. class Validate implements ValidateInterface
  14. {
  15. const TYPE_STRING = 'string';
  16. const TYPE_ARRAY = 'array';
  17. const TYPE_NUMBER = 'number';
  18. const TYPE_INTEGER = 'integer';
  19. const TYPE_FLOAT = 'float';
  20. const TYPE_OBJECT = 'object';
  21. const TYPE_ENUM = 'enum';
  22. const TYPE_URL = 'url';
  23. const TYPE_HEX = 'hex';
  24. const TYPE_EMAIL = 'email';
  25. const TYPE_DATE = 'date';
  26. const TRIGGER_CHANGE = 'change';
  27. const TRIGGER_BLUR = 'blur';
  28. const TRIGGER_SUBMIT = 'submit';
  29. protected $validate = [
  30. 'fields' => []
  31. ];
  32. protected $type;
  33. protected $trigger;
  34. /**
  35. * Validate constructor.
  36. * @param string $type
  37. * @param string $trigger
  38. */
  39. public function __construct($type, $trigger = self::TRIGGER_CHANGE)
  40. {
  41. $this->type($type);
  42. $this->trigger($trigger);
  43. }
  44. /**
  45. * @param string $trigger
  46. * @return $this
  47. */
  48. public function trigger($trigger)
  49. {
  50. $this->trigger = (string)$trigger;
  51. return $this;
  52. }
  53. /**
  54. * @param $type
  55. * @return $this
  56. */
  57. public function type($type)
  58. {
  59. $this->type = (string)$type;
  60. return $this;
  61. }
  62. public function set($validate)
  63. {
  64. array_merge($this->validate, $validate);
  65. if (!is_array($validate['fields'])) $validate['fields'] = [];
  66. return $this;
  67. }
  68. public function fields(array $fields)
  69. {
  70. $this->validate['fields'] = array_merge($this->validate['fields'], $fields);
  71. return $this;
  72. }
  73. public function field($field, $validate)
  74. {
  75. $this->validate['fields'][$field] = $validate;
  76. return $this;
  77. }
  78. /**
  79. * 必填
  80. *
  81. * @return $this
  82. */
  83. public function required()
  84. {
  85. $this->validate['required'] = true;
  86. return $this;
  87. }
  88. /**
  89. * 长度或值必须在这个范围内
  90. *
  91. * @param int|float $min
  92. * @param int|float $max
  93. * @return $this
  94. */
  95. public function range($min, $max)
  96. {
  97. $this->validate['min'] = (float)$min;
  98. $this->validate['max'] = (float)$max;
  99. return $this;
  100. }
  101. /**
  102. * 长度或值必须大于这个值
  103. *
  104. * @param int|float $min
  105. * @return $this
  106. */
  107. public function min($min)
  108. {
  109. $this->validate['min'] = (float)$min;
  110. return $this;
  111. }
  112. /**
  113. * 长度或值必须小于这个值
  114. *
  115. * @param int|float $max
  116. * @return $this
  117. */
  118. public function max($max)
  119. {
  120. $this->validate['max'] = (float)$max;
  121. return $this;
  122. }
  123. /**
  124. * 长度或值必须等于这个值
  125. *
  126. * @param int $length
  127. * @return $this
  128. */
  129. public function length($length)
  130. {
  131. $this->validate['len'] = (int)$length;
  132. return $this;
  133. }
  134. /**
  135. * 值必须在 list 中
  136. *
  137. * @param array $list
  138. * @return $this
  139. */
  140. public function enum(array $list)
  141. {
  142. $this->validate['enum'] = (int)$list;
  143. return $this;
  144. }
  145. /**
  146. * 错误信息
  147. * @param $message
  148. * @return $this
  149. */
  150. public function message($message)
  151. {
  152. $this->validate['message'] = (string)$message;
  153. return $this;
  154. }
  155. /**
  156. * 正则
  157. *
  158. * @param $pattern
  159. * @return $this
  160. */
  161. public function pattern($pattern)
  162. {
  163. $this->validate['pattern'] = (string)$pattern;
  164. return $this;
  165. }
  166. public function getValidate()
  167. {
  168. $validate = $this->validate;
  169. $validate['type'] = $this->type;
  170. $validate['trigger'] = $this->trigger;
  171. $fields = $validate['fields'];
  172. if (!($fieldCount = count($fields)) && count($validate) === 1)
  173. return [];
  174. if ($fieldCount) {
  175. foreach ($fields as $k => $field) {
  176. $fields[$k] = $field instanceof self ? $field->build() : $field;
  177. }
  178. $validate['fields'] = (object)$fields;
  179. } else {
  180. unset($validate['fields']);
  181. }
  182. return $validate;
  183. }
  184. }