Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

验证器对非必须参数不生效 #1266

Closed
dyf991645 opened this issue Apr 24, 2020 · 1 comment · Fixed by swoft-cloud/swoft-component#571
Closed

验证器对非必须参数不生效 #1266

dyf991645 opened this issue Apr 24, 2020 · 1 comment · Fixed by swoft-cloud/swoft-component#571
Assignees
Labels
swoft: validator issues for swoft validator component

Comments

@dyf991645
Copy link

版本2.0.9
比如

/**
 * @IsString(name="expire_at")
 * @NotEmpty()
 * @var string
 * protected $expireAt;
 */

如果参数中erpireAt == ""的话,只能通过验证的,也即NotEmpty()未生效。

看了源码,在vendor/swoft/validator/src/Validator.php中
第184行

if (!isset($data[$propName]) && !$property['required'] && !isset($property['type']['default'])) {
    continue;
}

此时

$propName == "expireAt"; 
!isset($data[$propName]) == true;
!$property['required'] == true; //非必须
!isset($property['type']['default']); //无默认值

条件成立,跳过此次循环,不会验证是否empty;
应该验证!isset($data[“expire_at”])而非!isset($data[“expireAt”])

将193-201行代码提前至180行即可解决。

@dyf991645
Copy link
Author

另外文档中说

属性的默认值就是参数的默认值,如果属性没有定义默认值,代表参数没有定义默认值且必须传递

实际没有默认值时也不是必传,必须加上@required()才行

inhere added a commit to swoft-cloud/swoft-component that referenced this issue Sep 13, 2020
inhere pushed a commit to swoft-cloud/swoft-validator that referenced this issue Sep 13, 2020
filx validator has no effect on not required param.
inhere added a commit to swoft-cloud/swoft-validator that referenced this issue Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swoft: validator issues for swoft validator component
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants