我使用Phalcon框架,我有一个集合模型,在这个模型的validate()
函数中,我验证我的字段如下:
class Users extends Collection
{
public function validation()
{
$this->validate(
new EmailValidator(
array(
"field" => "email",
"message" => "email is not valid"
)
)
);
$this->validate(
new NumericalityValidator(
array(
"field" => "phone",
"message" => "phone is not valid"
)
)
);
return $this->validationHasFailed() != true;
}
}
我如何tel to这个模型这两个字段中的一个是强制性的? 例如,如果tel不为空,电子邮件可以为空,或者如果电子邮件不为空,tel可以为空,当两个字段都为空时,验证应该失败。
答案 0 :(得分:0)
这样的东西?
public function validation()
{
if(empty($this->phone)) {
$this->validate(
new EmailValidator(
array(
"field" => "email",
"message" => "email is not valid"
)
)
);
}
if(empty($this->email)) {
$this->validate(
new NumericalityValidator(
array(
"field" => "phone",
"message" => "phone is not valid"
)
)
);
}
return $this->validationHasFailed() != true;
}
答案 1 :(得分:0)
您应该能够附加消息。 https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Message.html
<?php
use Phalcon\Mvc\Model\Message as Message;
public function validation()
{
if(empty($this->phone) && empty($this->email)) {
$message = new Message('Phone or email must be provided',
array('phone','email'),
'PresenceOf');
$this->appendMessage($message);
return $this->validationHasFailed() != true;
}