我從一個窗體(thread_title,thread_content和thread_tags)得到三個值:重構形式的代碼在PHP
這是代碼,以檢查它們的長度:
$errors = array();
$thread_title = filter_input(INPUT_POST, 'thread_title');
$thread_content = filter_input(INPUT_POST, 'thread_content');
$thread_tags = filter_input(INPUT_POST, 'thread_tags');
if (strlen($thread_title) < 20)
{
$errors['thread_title'] = 'Title is too short, minimum is 20 characters.';
}
if (strlen($thread_content) < 30)
{
$errors['thread_content'] = 'Content is too short, minimum is 30 characters.';
}
if (strlen($thread_tags) < 3)
{
$errors['thread_tags'] = 'Tags must be atleast 3 characters.';
}
我在答覆中重複這一點。 PHP文件:
if (strlen($reply_content) < 20)
{
$errors['reply_content'] = 'Reply must be atleast 20 characters.';
}
.etc
如果錯誤數組爲空,然後我清理數據和提交給數據庫。這個代碼如何更清潔,重構?
我知道我可以使用類似PEAR QUICK_FORM(2.0?)的東西,但是它仍然在alpha中,錯誤消息顯示爲JS彈出框,並且不在必填字段旁邊。
您可以在[CodeReview.SE](http://codereview.stackexchange.com)上發佈此信息,並可能獲得一些良好的反饋。 – BenV 2011-02-07 02:20:10