我想建立一个在线信息管理系统,它有一些中文输入。但是,ZF2的验证器无法验证中文。如何验证中文输入?要创建自定义验证器?如何创建一个?非常感谢!!!
There are actually 3 languages which are not accepted in their own script. These languages
are korean, japanese and **chinese** because this languages are using an alphabet where a
single character is build by using multiple characters.
In the case you are using these languages, the input will only be validated by using the
english alphabet.
答案 0 :(得分:1)
ZF2中没有中文或日文的验证器。
您可以做的是构建自己的小验证器类。我将Php check if the string has Chinese chars和How to check if the word is Japanese or English using PHP合并为extending the abstract validator为您构建这两个小验证器:
namespace MyApp\Validator;
use Zend\Validator\AbstractValidator;
// forgive the name of this class
class IsNotOrientalLanguage extends AbstractValidator
{
/**
* {@inheritDoc}
*/
public function isValid($value)
{
$this->abstractOptions['messages'] = array();
if (preg_match('/\p{Han}+/u', $value)) {
$this->abstractOptions['messages'][] = 'Chinese not allowed.';
}
if (preg_match('/[\x{4E00}-\x{9FBF}]/u', $value)) {
$this->abstractOptions['messages'][] = 'Kankji not allowed.';
}
if (preg_match('/[\x{3040}-\x{309F}]/u', $value)) {
$this->abstractOptions['messages'][] = 'Hiragana not allowed.';
}
if (preg_match('/[\x{30A0}-\x{30FF}]/u', $value)) {
$this->abstractOptions['messages'][] = 'Katakana not allowed.';
}
return ! $this->abstractOptions['messages'];
}
}
您现在可以在任何地方使用此验证器:
use Zend\InputFilter\Input;
use MyApp\Validator\IsNotOrientalLanguage;
$input = new Input('blog_post');
$input->getValidatorChain()->attach(new IsNotOrientalLanguage());