form-builder 升级1.2.4

This commit is contained in:
sugar1569 2018-11-14 10:42:50 +08:00
parent 26f3daba97
commit a6ab8ee153
3 changed files with 234 additions and 0 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

View File

@ -0,0 +1,185 @@
<?php
/**
* FormBuilder表单生成器
* Author: xaboy
* Github: https://github.com/xaboy/form-builder
*/
namespace FormBuilder\components;
use FormBuilder\interfaces\FormComponentInterFace;
class Validate implements FormComponentInterFace
{
const TYPE_STRING = 'string';
const TYPE_ARRAY = 'array';
const TYPE_NUMBER = 'number';
const TYPE_DATE = 'date';
const TRIGGER_CHANGE = 'change';
const TRIGGER_BLUR = 'blur';
protected $validate = [];
protected $type;
protected $trigger;
public function __construct($type, $trigger)
{
$this->type = $type;
$this->trigger = $trigger;
}
public static function str($trigger = self::TRIGGER_CHANGE)
{
return new static(self::TYPE_STRING,$trigger);
}
public static function arr($trigger = self::TRIGGER_CHANGE)
{
return new static(self::TYPE_ARRAY,$trigger);
}
public static function num($trigger = self::TRIGGER_CHANGE)
{
return new static(self::TYPE_NUMBER,$trigger);
}
public static function date($trigger = self::TRIGGER_CHANGE)
{
return new static(self::TYPE_DATE,$trigger);
}
protected function set($validate, $message = null)
{
$this->validate[] = $validate + [
'trigger' => $this->trigger,
'type' => $this->type,
'message' => $message
];
}
/**
* 必须为链接
* @param string|null $message
* @return $this
*/
public function url($message = null)
{
$this->set([
'type'=>'url'
],$message);
return $this;
}
/**
* 必须为邮箱
* @param string|null $message
* @return $this
*/
public function email($message = null)
{
$this->set([
'type'=>'email'
],$message);
return $this;
}
/**
* 必填
* @param string|null $message
* @return $this
*/
public function required($message = null)
{
$this->set([
'required' => true,
], $message);
return $this;
}
/**
* 长度或值必须在这个范围内
* @param int $min
* @param int $max
* @param string|null $message
* @return $this
*/
public function range($min, $max, $message = null)
{
$this->set([
'min' => (int)$min,
'max' => (int)$max,
], $message);
return $this;
}
/**
* 长度或值必须大于这个值
* @param int $min
* @param string|null $message
* @return $this
*/
public function min($min, $message = null)
{
$this->set([
'min' => (int)$min,
], $message);
return $this;
}
/**
* 长度或值必须小于这个值
* @param int $max
* @param string|null $message
* @return $this
*/
public function max($max, $message = null)
{
$this->set([
'max' => (int)$max,
], $message);
return $this;
}
/**
* 长度或值必须等于这个值
* @param int $length
* @param string|null $message
* @return $this
*/
public function length($length, $message = null)
{
$this->set([
'length' => (int)$length
], $message);
return $this;
}
/**
* 值必须在 list
* @param array $list
* @param string|null $message
* @return $this
*/
public function enum($list, $message = null)
{
$this->set([
'type'=>'enum',
'enum' => (array)$list
], $message);
return $this;
}
public function build()
{
return $this->validate;
}
}

View File

@ -0,0 +1,49 @@
<?php
/**
* FormBuilder表单生成器
* Author: xaboy
* Github: https://github.com/xaboy/form-builder
*/
namespace FormBuilder\traits\form;
use FormBuilder\components\Validate;
trait FormValidateTrait
{
public static function validateStr()
{
return Validate::str();
}
public static function validateInput()
{
return Validate::str(Validate::TRIGGER_BLUR);
}
public static function validateArr()
{
return Validate::arr();
}
public static function validateNum()
{
return Validate::num();
}
public static function validateNumInput()
{
return Validate::num(Validate::TRIGGER_BLUR);
}
public static function validateDate()
{
return Validate::date();
}
public static function validateFrame()
{
return self::ValidateArr();
}
}