php 尊重验证如果不为空则验证
php respect validation just validate if not empty
我正在使用 php 尊重验证。
https://github.com/Respect/Validation
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::email())
->attribute('mobile', v::numeric()->length(10,12))
->attribute('firstName', v::stringType()->length(5,255))
->attribute('lastName', v::stringType()->length(5,255))
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::stringType()->length(8,16));
$userValidator->assert($object);
}
}
这是我的用户验证码。
用户名和密码只是必填字段。
我希望它们是必需的,并且仅当用户填写了输入时才应应用其他字段验证。
我应该怎么办 ?
毕竟我已经实施了自己的规则(唯一的)
如果有必要,我准备制定新的规则或其他任何东西。
另外,如果您更了解 php 用于输入验证的包,我会洗耳恭听。
我没有用过这个验证包。这可能不是最好的答案,但这是我从包文档中得到的。
我认为方法 oneOf() would help you, as it acts as an OR operator. The other method that would help, is nullType() 验证输入是否为空。您可以将您拥有的内容与 nullType()
分组,使该字段成为可选字段。
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::email())
->attribute('mobile', v::oneOf(
v::numeric()->length(10,12),
v::nullType
))
->attribute('firstName', v::oneOf(
v::stringType()->length(5,255),
v::nullType
))
->attribute('lastName', v::oneOf(
v::stringType()->length(5,255),
v::nullType
))
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::oneOf(
v::stringType()->length(8,16),
v::nullType
));
$userValidator->assert($object);
}
}
我还没有测试过,但我认为它会起作用。
我知道这已经过时了,但我 运行 进入了这个问题,有一种更简单的方法可以做你想做的事:
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::email(), false)
->attribute('mobile', v::numeric()->length(10,12), false)
->attribute('firstName', v::stringType()->length(5,255), false)
->attribute('lastName', v::stringType()->length(5,255), false)
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::stringType()->length(8,16));
$userValidator->assert($object);
}
}
在 attribute() 方法中有一个可选的第三个参数来决定是否强制验证:
https://github.com/Respect/Validation/blob/master/docs/Attribute.md
这是一个老问题,但 optional() 方法会起作用。
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::optional(v::email()))
->attribute('mobile', v::optional(v::numeric()->length(10,12)))
->attribute('firstName', v::optional(v::stringType()->length(5,255)))
->attribute('lastName', v::optional(v::stringType()->length(5,255)))
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::stringType()->length(8,16));
$userValidator->assert($object);
}
}
查看文档:https://github.com/Respect/Validation/blob/1.1/docs/Optional.md
我正在使用 php 尊重验证。 https://github.com/Respect/Validation
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::email())
->attribute('mobile', v::numeric()->length(10,12))
->attribute('firstName', v::stringType()->length(5,255))
->attribute('lastName', v::stringType()->length(5,255))
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::stringType()->length(8,16));
$userValidator->assert($object);
}
}
这是我的用户验证码。
用户名和密码只是必填字段。 我希望它们是必需的,并且仅当用户填写了输入时才应应用其他字段验证。 我应该怎么办 ? 毕竟我已经实施了自己的规则(唯一的) 如果有必要,我准备制定新的规则或其他任何东西。 另外,如果您更了解 php 用于输入验证的包,我会洗耳恭听。
我没有用过这个验证包。这可能不是最好的答案,但这是我从包文档中得到的。
我认为方法 oneOf() would help you, as it acts as an OR operator. The other method that would help, is nullType() 验证输入是否为空。您可以将您拥有的内容与 nullType()
分组,使该字段成为可选字段。
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::email())
->attribute('mobile', v::oneOf(
v::numeric()->length(10,12),
v::nullType
))
->attribute('firstName', v::oneOf(
v::stringType()->length(5,255),
v::nullType
))
->attribute('lastName', v::oneOf(
v::stringType()->length(5,255),
v::nullType
))
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::oneOf(
v::stringType()->length(8,16),
v::nullType
));
$userValidator->assert($object);
}
}
我还没有测试过,但我认为它会起作用。
我知道这已经过时了,但我 运行 进入了这个问题,有一种更简单的方法可以做你想做的事:
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::email(), false)
->attribute('mobile', v::numeric()->length(10,12), false)
->attribute('firstName', v::stringType()->length(5,255), false)
->attribute('lastName', v::stringType()->length(5,255), false)
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::stringType()->length(8,16));
$userValidator->assert($object);
}
}
在 attribute() 方法中有一个可选的第三个参数来决定是否强制验证:
https://github.com/Respect/Validation/blob/master/docs/Attribute.md
这是一个老问题,但 optional() 方法会起作用。
class UserValidator implements iValidator
{
public function validate($object)
{
$userValidator = v::attribute('email', v::optional(v::email()))
->attribute('mobile', v::optional(v::numeric()->length(10,12)))
->attribute('firstName', v::optional(v::stringType()->length(5,255)))
->attribute('lastName', v::optional(v::stringType()->length(5,255)))
->attribute('userName', v::stringType()->length(5,255)->unique('users'))
->attribute('password', v::stringType()->length(8,16));
$userValidator->assert($object);
}
}
查看文档:https://github.com/Respect/Validation/blob/1.1/docs/Optional.md