想更方便的话
,你可以使用Factory模式或者自动填充的方法来从$_POST里面提取关键字
。 classPost{
//...
function&autoFill(){
$ret=&newPost;
foreach($_POSTas$key=>$value){
$ret->set($key,$value);
}
return$ret;
}
}
使用这个Post类
,你可以编辑你的FormHandler::build()方法,默认使用已经存在的$_post数据:
classFormHandler{
functionbuild(&$post){
returnarray(
newLabeled(‘FirstName’
,newTextInput(‘fname’,$post->get(‘fname’)))
,newLabeled(‘LastName’
,newTextInput(‘lname’,$post->get(‘lname’)))
,newLabeled(‘Email’
,newTextInput(‘email’,$post->get(‘email’)))
);
}
}
现在你可以创建一个php脚本使用FormHandler类来产生HTML表单:
<formaction=”formpage.php”method=”post”>
<?php
210TheDecoratorPattern
$post=&Post::autoFill();
$form=FormHandler::build($post);
foreach($formas$widget){
echo$widget->paint(),“<br>
”;
}
?>
<inputtype=”submit”value=”Submit”>
</form>
现在,你已经拥有了一个提交给它自身并且能保持posted数据的表单处理(formhandler)类
。 现在。我们继续为表单添加一些验证机制。方法是编辑另一个组件装饰器类来表达一个“invalid”状态并扩展FormHandler类增加一个validate()方法以处理组件示例数组。如果组件非法(“invalid”),我们通过一个“invalid”类将它包装在<span>元素中。这里是一个证明这个目标的测试
classWidgetTestCaseextendsUnitTestCase{
//...
functiontestInvalid(){
$text=&newInvalid(
newTextInput(‘email’));
$output=$text->paint();
$this->assertWantedPattern(
‘~^<spanclass=”invalid”><input[^>]+></span>$~i’,$output);
}
}
这里是InvalidWidgetDecorator子类:
//代码Here’stheInvalidWidgetDecoratorsubclass:
classInvalidextendsWidgetDecorator{
functionpaint(){
return‘<spanclass=”invalid”>’.$this->widget->paint().’</span>’;
}
}
希望这篇
php教程:php设计模式介绍之装饰器模式(4)的文章能够对您有所帮助,如果您觉得这篇网站维护教程有用的话,别忘了推荐给您的朋友哦!如果您有好的经验方法,不妨拿出来和大家一起分享:假如每个人都拿出一个经验,那么我们都将额外的获取一堆他人的经验。
请记住本站永久域名:(黑客防线网安服务器维护方案维护基地)Www.Rongsen.Com.Cn