【发布时间】:2015-06-19 02:16:10
【问题描述】:
我有个快速的问题让我头疼。
我正在尝试使用 PHP 中的方法链制作表单验证系统
我想要做的是能够调用例如(请检查代码cmets):
$firstname = $OBJECT->Forms->Field("First Name", "firstname"); //This one doesn't validate, but just puts what's on firstname field on to $firstname. But this way doesn't work for me, because I have to return the object so it can be chainable and not the variable of the POST. How can I do this?
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate(); //this one validates if the field is not empty and if it's empty it'll insert the first parameter ("First Name") onto an array to display the errors.
$email = $OBJECT->Forms->Field("Email", "email")->Validate()->Email(); //This one does the same as above but validates Email and inserts the value of the email field onto $email
but I prefer the next one...
$email = $OBJECT->Forms->Field("Email", "email")->Validate->Email(); //I'd rather prefer this method but I don't know how to do it without using the parenthesis on the Validate method.
我只能让它这样工作
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate();
and
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate()->Email();
没有->Validate();,我似乎无法让它工作(像这样:$firstname = $OBJECT->Forms->Field("First Name", "firstname");)
分享的代码有点乱。但是代码很简单……我有一个forms.class.php 和一个validate.class.php。 forms.class.php 从 validate.class.php 创建 Validate 类的实例,Forms 对象通过构造函数上的 Validate 类传递。
我希望能够做到:
$OBJECT->Forms->Field();
$OBJECT->Forms->Field()->Validate();
$OBJECT->Forms->Field()->Validate()->Email;
$OBJECT->Forms->Field()->Validate()->Telephone;
或者这个更喜欢:
$OBJECT->Forms->Field();
$OBJECT->Forms->Field()->Validate;
$OBJECT->Forms->Field()->Validate->Email;
$OBJECT->Forms->Field()->Validate->Telephone;
只是想通了:
$OBJECT->Forms->Field()->Validate();
$OBJECT->Forms->Field()->Validate()->Email();
$OBJECT->Forms->Field()->Validate()->Telephone();
但是任何形式都可以
谢谢。
【问题讨论】:
-
你的班级是什么样的?
-
这是一个 PHP 框架,代码中等大小。但如果你愿意,我可以发布它。但我真的很想要一些关于如何实现这一目标的例子。谢谢
-
这感觉像是相关阅读:stackoverflow.com/q/12351737/2943403