Skip to content

Commit c45e40a

Browse files
committed
bug fixed,update
1 parent 07a5bd5 commit c45e40a

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

src/ValidationTrait.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ trait ValidationTrait
7171
*/
7272
private $_hasValidated = false;
7373

74+
private $_position = [
75+
'attr' => 0,
76+
'validator' => 1,
77+
];
78+
7479
/**
7580
* @return array
7681
*/
@@ -215,37 +220,43 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
215220
*/
216221
protected function doValidate($data, $name, $validator, $copy)
217222
{
218-
$result = ValidatorList::required($data, $name);
223+
if ( !isset($data[$name]) ) {
224+
return [false, $validator instanceof \Closure ? 'callback' : $validator];
225+
}
219226

220-
if ($result && $validator !== 'required') {
221-
array_unshift($copy, $data[$name]);// 压入当前属性值
227+
if ( $validator === 'required' ) {
228+
$result = ValidatorList::required($data, $name);
222229

223-
// if it's a closure
224-
if ( is_callable($validator) && $validator instanceof \Closure) {
225-
$callback = $validator;
226-
$validator = 'callback';
230+
return [$result,$validator];
231+
}
227232

228-
// if it is a custom add callback in the property {@see $_validators}.
229-
} elseif ( is_string($validator) && isset($this->_validators['validator']) ) {
233+
array_unshift($copy, $data[$name]);// 压入当前属性值
230234

231-
$callback = $this->_validators['validator'];
235+
// if it's a closure
236+
if ( is_callable($validator) && $validator instanceof \Closure) {
237+
$callback = $validator;
238+
$validator = 'callback';
232239

233-
// if it is a custom method of the subclass.
234-
} elseif ( is_string($validator) && method_exists($this, $validator) ) {
240+
// if it is a custom add callback in the property {@see $_validators}.
241+
} elseif ( is_string($validator) && isset($this->_validators['validator']) ) {
235242

236-
$callback = [ $this, $validator ];
243+
$callback = $this->_validators['validator'];
237244

238-
// it's a method of the class 'ValidatorList'
239-
} elseif ( is_string($validator) && is_callable([ValidatorList::class, $validator]) ) {
245+
// if it is a custom method of the subclass.
246+
} elseif ( is_string($validator) && method_exists($this, $validator) ) {
240247

241-
$callback = [ ValidatorList::class, $validator];
242-
} else {
243-
throw new \InvalidArgumentException("validator [$validator] don't exists!");
244-
}
248+
$callback = [ $this, $validator ];
245249

246-
$result = call_user_func_array($callback, $copy);
250+
// it's a method of the class 'ValidatorList'
251+
} elseif ( is_string($validator) && is_callable([ValidatorList::class, $validator]) ) {
252+
253+
$callback = [ ValidatorList::class, $validator];
254+
} else {
255+
throw new \InvalidArgumentException("validator [$validator] don't exists!");
247256
}
248257

258+
$result = call_user_func_array($callback, $copy);
259+
249260
return [$result,$validator];
250261
}
251262

@@ -288,6 +299,12 @@ protected function collectRules()
288299

289300
// 循环规则, 搜集当前场景的规则
290301
foreach ($this->getRules() as $rule) {
302+
303+
// check validator
304+
if ( !is_string($rule[1]) && !($rule[1] instanceof \Closure) ) {
305+
throw new \InvalidArgumentException("validator setting error!");
306+
}
307+
291308
if ( empty($rule['scene']) ) {
292309
$availableRules[] = $rule;
293310
} else {

src/ValidatorList.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function inputHasVar($type, $varName)
8383
*/
8484
public static function required($data, $required)
8585
{
86-
return isset($data[$required]) && $data[$required]!=='';
86+
return isset($data[$required]) && $data[$required]!=='' && $data[$required]!==null;
8787
}
8888

8989
/**
@@ -103,6 +103,10 @@ public static function required($data, $required)
103103
*/
104104
public static function integer($int, array $options=[], $flags=0)
105105
{
106+
if (!is_numeric($int)) {
107+
return false;
108+
}
109+
106110
$settings = [];
107111

108112
if ($options) {
@@ -143,10 +147,14 @@ public static function size($var, $min = null, $max = null)
143147
{
144148
$options = [];
145149

146-
if (is_string($var)) {
150+
if (is_numeric($var)) {
151+
$var = (int)$var;
152+
} elseif (is_string($var)) {
147153
$var = StrHelper::strlen($var);
148154
} elseif (is_array($var)) {
149155
$var = count($var);
156+
} else {
157+
return false;
150158
}
151159

152160
if ( is_numeric($min) ) {

0 commit comments

Comments
 (0)