| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /**
- * PHP表单生成器
- *
- * @package FormBuilder
- * @author xaboy <xaboy2005@qq.com>
- * @version 2.0
- * @license MIT
- * @link https://github.com/xaboy/form-builder
- */
- namespace FormBuilder\UI\Iview;
- use FormBuilder\Contract\ValidateInterface;
- class Validate implements ValidateInterface
- {
- const TYPE_STRING = 'string';
- const TYPE_ARRAY = 'array';
- const TYPE_NUMBER = 'number';
- const TYPE_INTEGER = 'integer';
- const TYPE_FLOAT = 'float';
- const TYPE_OBJECT = 'object';
- const TYPE_ENUM = 'enum';
- const TYPE_URL = 'url';
- const TYPE_HEX = 'hex';
- const TYPE_EMAIL = 'email';
- const TYPE_DATE = 'date';
- const TRIGGER_CHANGE = 'change';
- const TRIGGER_BLUR = 'blur';
- const TRIGGER_SUBMIT = 'submit';
- protected $validate = [
- 'fields' => []
- ];
- protected $type;
- protected $trigger;
- /**
- * Validate constructor.
- * @param string $type
- * @param string $trigger
- */
- public function __construct($type, $trigger = self::TRIGGER_CHANGE)
- {
- $this->type($type);
- $this->trigger($trigger);
- }
- /**
- * @param string $trigger
- * @return $this
- */
- public function trigger($trigger)
- {
- $this->trigger = (string)$trigger;
- return $this;
- }
- /**
- * @param $type
- * @return $this
- */
- public function type($type)
- {
- $this->type = (string)$type;
- return $this;
- }
- public function set($validate)
- {
- array_merge($this->validate, $validate);
- if (!is_array($validate['fields'])) $validate['fields'] = [];
- return $this;
- }
- public function fields(array $fields)
- {
- $this->validate['fields'] = array_merge($this->validate['fields'], $fields);
- return $this;
- }
- public function field($field, $validate)
- {
- $this->validate['fields'][$field] = $validate;
- return $this;
- }
- /**
- * 必填
- *
- * @return $this
- */
- public function required()
- {
- $this->validate['required'] = true;
- return $this;
- }
- /**
- * 长度或值必须在这个范围内
- *
- * @param int|float $min
- * @param int|float $max
- * @return $this
- */
- public function range($min, $max)
- {
- $this->validate['min'] = (float)$min;
- $this->validate['max'] = (float)$max;
- return $this;
- }
- /**
- * 长度或值必须大于这个值
- *
- * @param int|float $min
- * @return $this
- */
- public function min($min)
- {
- $this->validate['min'] = (float)$min;
- return $this;
- }
- /**
- * 长度或值必须小于这个值
- *
- * @param int|float $max
- * @return $this
- */
- public function max($max)
- {
- $this->validate['max'] = (float)$max;
- return $this;
- }
- /**
- * 长度或值必须等于这个值
- *
- * @param int $length
- * @return $this
- */
- public function length($length)
- {
- $this->validate['len'] = (int)$length;
- return $this;
- }
- /**
- * 值必须在 list 中
- *
- * @param array $list
- * @return $this
- */
- public function enum(array $list)
- {
- $this->validate['enum'] = (int)$list;
- return $this;
- }
- /**
- * 错误信息
- * @param $message
- * @return $this
- */
- public function message($message)
- {
- $this->validate['message'] = (string)$message;
- return $this;
- }
- /**
- * 正则
- *
- * @param $pattern
- * @return $this
- */
- public function pattern($pattern)
- {
- $this->validate['pattern'] = (string)$pattern;
- return $this;
- }
- public function getValidate()
- {
- $validate = $this->validate;
- $validate['type'] = $this->type;
- $validate['trigger'] = $this->trigger;
- $fields = $validate['fields'];
- if (!($fieldCount = count($fields)) && count($validate) === 1)
- return [];
- if ($fieldCount) {
- foreach ($fields as $k => $field) {
- $fields[$k] = $field instanceof self ? $field->build() : $field;
- }
- $validate['fields'] = (object)$fields;
- } else {
- unset($validate['fields']);
- }
- return $validate;
- }
- }
|