私は最近、自分用に非常に単純な MVC 風のフレームワークを作成しました。現在、そのフレームワークをいくつかのクラスで拡張しようとしているので、将来的に時間を節約できます。私が今作成しようとしているクラスは、フォーム検証クラスです。文字列の最小長をチェックする関数を作成するまでは、かなりうまく機能していると思いました。
クラス :
<?php
/**
* Form Validation Class
*/
class formValidation
{
private $errorMessages = array();
private $method;
public $errorString;
public $validationRules = array();
public function setMethod($method)
{
$this->method = $method;
}
public function setRules($rules)
{
$this->validationRules = $rules;
}
public function run()
{
if (!empty($this->method)) {
foreach ($this->method as $fieldname => $fieldvalue) {
foreach ($this->validationRules as $rule) {
if ($rule['field'] == $fieldname) {
foreach ($rule as $option => $rulevalue) {
switch ($option) {
case 'required':
if (!$this->checkRequired($fieldvalue)) {
$this->errorMessages[] = $rule['name'] . " is a required field";
}
break;
case 'letters_numbers':
if (!$this->checkLettersNumbers($fieldvalue)) {
$this->errorMessages[] = $rule['name'] . " may only contain letters and numbers";
}
case 'min_length':
if (!$this->checkMinLength($fieldvalue, $rulevalue)) {
$this->errorMessages[] = $rule['name'] . " must contain at least $rulevalue characters";
}
}
}
}
}
}
if (!empty($this->errorMessages[0])) {
return false;
} else {
return true;
}
} else {
return false;
}
}
// Checks
private function checkRequired($value) // Makes a field mandatory
{
if ($value == "") {
return false;
} else {
return true;
}
}
private function checkLettersNumbers($value) // Allow Letters and Numbers only
{
return preg_match('/^[A-Za-z\s ]+$/', $value);
}
private function checkMinLength($value, $minlength) // Check input for minimum length
{
if (strlen($value) < $minlength) {
return false;
} else {
return true;
}
}
// Error Handling Functions
private function makeErrorString($prelimiter = '<li>', $delimiter = '</li>') // Build all the errors into a string
{
if (!empty($this->errorMessages[0])) {
$errors = "";
foreach ($this->errorMessages as $message) {
$errors .= $prelimiter;
$errors .= $message;
$errors .= $delimiter;
}
$this->errorString = $errors;
return true;
}
}
public function ValidationErrors() // Return the error string
{
if ($this->makeErrorString()) {
return $this->errorString;
}
}
/**
* End of Class
**/
}
/**
* End of formValidation.php
**/
?>
そして私のコントローラーファイル:
<?php
/**
* Homepage Controller
* @copyright 2012
*/
class home extends SimpleController
{
public function index()
{
$view = new view('home');
$val = new formValidation();
$val->setMethod($_POST);
$rules = array(
array(
'field' => 'test',
'name' => 'Test',
'required' => true,
'letters_numbers' => true,
'min_length' => '5'
),
array(
'field' => 'bla',
'name' => 'Trololol',
'required' => true
)
);
$val->setRules($rules);
if (!$val->run()) {
echo $val->ValidationErrors();
}
}
}
?>
私の見解では、test と bla の 2 つのフィールドを持つフォームがあります。空のフォームを送信すると、checkMinLength() 関数は 2 つの値を返します。
テストには少なくとも 1 文字が含まれている必要があります テストには少なくとも 5 文字が含まれている必要があります
まず第一に、その 1 はどこから取得され、2 つ目はなぜ同じ関数に対して 2 つのメッセージが表示されるのですか? 理由がわかりません。
皆さんが私を助けてくれることを願っています! (コントローラーのエコーはデモ用です)
ありがとう!