使用 phalcon 验证进行不区分大小写的验证 class
case-insensitive validation using phalcon validation class
在验证中 class 我的 intiialize
函数是这样的
public function initialize()
{
$this->add('gender',new InclusionIn(array(
'message' => 'Please enter a valid Gender',
'domain' => array('Male','Female'),
'case_insensitive' => false
)));
}
问题是,InclusionIn
进行了区分大小写的验证,因此如果用户输入 "male" 应用程序会抛出错误 请输入有效的性别 。我希望这个验证应该是 case-insensitive
但我没有找到任何方法来做到这一点。
InclusionIn
使用区分大小写的 in_array
。您必须自己编写 Validator
才能获得所需的功能。 Here 是 class 的实现,因此您可以查看可用的选项。
另一个选项是在触发验证器之前格式化输入。例如如果他们输入 male
或 MALE
将其转换为 Male
这样验证就会通过。
按照@M2sh 和@honerlawd 的指导,最后我写了一个新的“inclusion”验证器,在这里分享代码
<?php
namespace library\app\validators;
use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Message;
class InclusionIn extends Validator
{
/**
* Executes the validation
*/
public function validate(Validation $validation, $attribute)
{
$value = $validation->getValue($attribute);
if ($this->isSetOption("allowEmpty") && empty($value)) {
return true;
}
/**
* A domain is an array with a list of valid values
*/
$domain = $this->getOption("domain");
if (!is_array($domain)) {
throw new Exception("Option 'domain' must be an array");
}
$refinedDomain = array_map('strtolower', $domain);
$strict = false;
if ($this->isSetOption("strict")) {
if (!is_bool($strict)) {
throw new Exception("Option 'strict' must be a boolean");
}
$strict = $this->getOption("strict");
}
/**
* Check if the value is contained by the array
*/
if (!in_array(strtolower($value), $refinedDomain, $strict)) {
$label = $this->getOption("label");
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption("message");
$replacePairs = ['field' => $label, 'domain' => join(", ", $domain)];
if (empty($message)) {
$message = $validation->getDefaultMessage("InclusionIn");
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, "InclusionIn"));
return false;
}
return true;
}
}
在验证模型的情况下,您可以使用 beforeValidation()
、beforeValidationOnCreate()
或 beforeValidationOnUpdate()
到 lowercase/uppercase 值:
function beforeValidation() {
$this->gender = ucfirst($this->gender);
}
如果通过 Forms 验证,您可以应用 addFilter(lower)
并验证小写字母,例如
您现在可以使用过滤器,请参阅 phalcon 过滤和消毒 https://docs.phalconphp.com/en/latest/reference/filter.html
在这样的控制器中:注意过滤器需要添加到服务初始化中
$gender = $this->filter->sanitize($gender, "lower");
或
在您的自定义验证中 class:
class MyValidation extends Validation
{
public function initialize()
{
$this->add('gender',new InclusionIn(array(
'message' => 'Please enter a valid Gender',
'domain' => array('male','female'),
)));
$this->setFilters('gender', 'lower');
}
}
在验证中 class 我的 intiialize
函数是这样的
public function initialize()
{
$this->add('gender',new InclusionIn(array(
'message' => 'Please enter a valid Gender',
'domain' => array('Male','Female'),
'case_insensitive' => false
)));
}
问题是,InclusionIn
进行了区分大小写的验证,因此如果用户输入 "male" 应用程序会抛出错误 请输入有效的性别 。我希望这个验证应该是 case-insensitive
但我没有找到任何方法来做到这一点。
InclusionIn
使用区分大小写的 in_array
。您必须自己编写 Validator
才能获得所需的功能。 Here 是 class 的实现,因此您可以查看可用的选项。
另一个选项是在触发验证器之前格式化输入。例如如果他们输入 male
或 MALE
将其转换为 Male
这样验证就会通过。
按照@M2sh 和@honerlawd 的指导,最后我写了一个新的“inclusion”验证器,在这里分享代码
<?php
namespace library\app\validators;
use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Message;
class InclusionIn extends Validator
{
/**
* Executes the validation
*/
public function validate(Validation $validation, $attribute)
{
$value = $validation->getValue($attribute);
if ($this->isSetOption("allowEmpty") && empty($value)) {
return true;
}
/**
* A domain is an array with a list of valid values
*/
$domain = $this->getOption("domain");
if (!is_array($domain)) {
throw new Exception("Option 'domain' must be an array");
}
$refinedDomain = array_map('strtolower', $domain);
$strict = false;
if ($this->isSetOption("strict")) {
if (!is_bool($strict)) {
throw new Exception("Option 'strict' must be a boolean");
}
$strict = $this->getOption("strict");
}
/**
* Check if the value is contained by the array
*/
if (!in_array(strtolower($value), $refinedDomain, $strict)) {
$label = $this->getOption("label");
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption("message");
$replacePairs = ['field' => $label, 'domain' => join(", ", $domain)];
if (empty($message)) {
$message = $validation->getDefaultMessage("InclusionIn");
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, "InclusionIn"));
return false;
}
return true;
}
}
在验证模型的情况下,您可以使用 beforeValidation()
、beforeValidationOnCreate()
或 beforeValidationOnUpdate()
到 lowercase/uppercase 值:
function beforeValidation() {
$this->gender = ucfirst($this->gender);
}
如果通过 Forms 验证,您可以应用 addFilter(lower)
并验证小写字母,例如
您现在可以使用过滤器,请参阅 phalcon 过滤和消毒 https://docs.phalconphp.com/en/latest/reference/filter.html
在这样的控制器中:注意过滤器需要添加到服务初始化中
$gender = $this->filter->sanitize($gender, "lower");
或
在您的自定义验证中 class:
class MyValidation extends Validation
{
public function initialize()
{
$this->add('gender',new InclusionIn(array(
'message' => 'Please enter a valid Gender',
'domain' => array('male','female'),
)));
$this->setFilters('gender', 'lower');
}
}