将 php 验证器优化为 OOP
我需要一个 php 验证器类来验证用户输入。
我希望它能够接受字段的关联数组 =>像这样的值:
array(
"username" => "Alex",
"email_address" => "@@#3423£[email protected]"
);
然后返回一个像这样的错误数组:
array(
"username" => "",
"email_address" => "Invalid Email Address"
);
但我真的很苦恼我到底要怎么做!
我读过无数关于 PHP 验证器的页面,并了解到做到这一点的最佳方法是使用策略模式。但我不知道怎么办??
就像......这就是我到目前为止所得到的:
class Validator {
private
$_errors,
$_fields,
static private $_map = array (
"firstname" => "name",
"surname" => "name",
"agency_name" => "name",
"agency_office" => "name",
"username" => "username",
"email_address" => "email_address",
);
public function __construct( array $fields ) {
$this->_fields = $fields;
}
public function validate() {
foreach ( $this->_fields as $field => $value ) {
if ( method_exists( __CLASS__, self::$_map[$field] ) ) {
if ( in_array( $field, self::$_map ) ) {
$this->{self::$_map[$field]}( $field, $value );
}
}
else {
die( " Unable to validate field $field" );
}
}
}
public function get_errors() {
return $this->_errors;
}
private function name( $field, $value ) {
if ( !preg_match( "/^[a-zA-Z]{2,50}$/", $value ) ) {
$this->errors[$field] = "Invalid. Must be 2 to 50 alphanumerical characters";
}
}
private function username( $field, $value ) {
if ( !preg_match( "/^[a-zA-Z0-9_\-]{10,50}$/", $value ) ) {
$this->errors[$field] = "Invalid. Must be 10 to 50 characters. Can contain digits, characters, _ (underscore) and - (hyphen)";
}
}
private function password( $field, $value ) {
if ( !preg_match( "/^[a-zA-Z0-9\.\-]{8,30}$/", $value ) ) {
$this->_errors[$field] = "Invalid. Must be 8 to 30 characters. Can contain digits, characters, . (full stop) and - (hyphen)";
}
}
private function email_address( $field, $value ) {
if ( !filter_var( $value, FILTER_VALIDATE_EMAIL ) ) {
$this->_errors[$field] = "Invalid Email Address";
}
}
}
问题是,它甚至没有考虑类似的数据库连接,已经注册的用户名,
密码不匹配
而且与我刚刚让编码器阻止的 那一刻及其在内心摧毁了我:(
任何人都可以解释一下所需的类和每个类需要执行的功能吗?
我真的需要输入和输出采用已经解释过的格式!
非常感谢互联网人们!
I need a php validator class that validates user inputs.
I want it to be able to accept an assoc array of fields => values like:
array(
"username" => "Alex",
"email_address" => "@@#3423£[email protected]"
);
and then return an array of errors like this:
array(
"username" => "",
"email_address" => "Invalid Email Address"
);
But I'm really struggling on HOW the hell I'm going to do this!
I've read countless pages on PHP validators and read that the best way to do this is with the strategy pattern. But i dont know how??
Like... This is what I've got so far:
class Validator {
private
$_errors,
$_fields,
static private $_map = array (
"firstname" => "name",
"surname" => "name",
"agency_name" => "name",
"agency_office" => "name",
"username" => "username",
"email_address" => "email_address",
);
public function __construct( array $fields ) {
$this->_fields = $fields;
}
public function validate() {
foreach ( $this->_fields as $field => $value ) {
if ( method_exists( __CLASS__, self::$_map[$field] ) ) {
if ( in_array( $field, self::$_map ) ) {
$this->{self::$_map[$field]}( $field, $value );
}
}
else {
die( " Unable to validate field $field" );
}
}
}
public function get_errors() {
return $this->_errors;
}
private function name( $field, $value ) {
if ( !preg_match( "/^[a-zA-Z]{2,50}$/", $value ) ) {
$this->errors[$field] = "Invalid. Must be 2 to 50 alphanumerical characters";
}
}
private function username( $field, $value ) {
if ( !preg_match( "/^[a-zA-Z0-9_\-]{10,50}$/", $value ) ) {
$this->errors[$field] = "Invalid. Must be 10 to 50 characters. Can contain digits, characters, _ (underscore) and - (hyphen)";
}
}
private function password( $field, $value ) {
if ( !preg_match( "/^[a-zA-Z0-9\.\-]{8,30}$/", $value ) ) {
$this->_errors[$field] = "Invalid. Must be 8 to 30 characters. Can contain digits, characters, . (full stop) and - (hyphen)";
}
}
private function email_address( $field, $value ) {
if ( !filter_var( $value, FILTER_VALIDATE_EMAIL ) ) {
$this->_errors[$field] = "Invalid Email Address";
}
}
}
The problems with this is, it doesn't even consider database connections for like, already registered usernames,
Also is doesn't match passwords
I've just got coders block at the moment and its destroying me on the inside :(
Can anybody give a an explaination of the classes required and functions each class will need to do?
I really need the inputs and outputs to be in the format already explained though!
Thankyou Very Much Internet People!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为我的 MVC 的一部分,我解决了同样的问题。我可以给你一个清单,但用几行话试着描述一下如何做。
我有 3 个基类
Form
、Validator
、Field
,这些类的每个对象都通过一个 YAML 文件进行配置,结构如下: ,让我们从
Form
开始,当对象构造表单时,表单会采用上述 YAML 文件,并且由于该配置会创建字段。像这样的事情:现在我们有了带有字段和每个字段的验证器的表单,然后为了验证我使用这种机制:
表单遍历每个字段,调用
validate()
方法,Field(获取绑定值)调用绑定Validator的validate($value)方法,传递存储的值。在此方法中,验证器调用
validateOption()
方法,其中每个选项都有一个简单的开关,例如:在这里您可以看到对所需选项的验证。如果我需要更多验证器,我可以扩展 Base 验证器的类,定义更多选项,并重新定义
validateOption()
,其中位于选项switch 的
放置default
语句中parent::validateOption()
。因此,指定的选项在新类中验证,而旧类在基本验证器类中验证。如果有任何问题...不客气。
As a part of the my MVC I have solved the same problem. I could give you a listing, but in a few lines try to describe how.
I got 3 base classes
Form
,Validator
,Field
, each of object of this classes configuring through one YAML file, structured somehow like this:So, lets start with
Form
, when object is constructing the form takes the YAML file described above, and due to that configuration creates fields. Something like this:So, now we have form with fields and validator for each field, then to validate I use this mechanism:
Form goes through each field, calling
validate()
method,Field (got the binded value) call
validate($value)
method of binded Validator, passing the stored value. Inside this method Validator calls thevalidateOption()
method, in which there is a simple switch for each options, for example:Here you can see validating on required option. If I need more validators, I extend class of the Base validator, defined few more options, and redefine
validateOption()
, where indefault
statement of the option'sswitch
putparent::validateOption()
. So specified options validates in new class, and old one in base validator Class.If there any questions... You're welcome.