2014-02-24 36 views
-2

我在論壇上查找了類似的問題,但找不到任何能幫助我的東西。在字段旁邊顯示Php表單錯誤

我有驗證類驗證表單條目。但它將它們顯示在表格的頂部。我怎麼能顯示這些字段旁邊的字段,而不是窗體的頂部?

registration.php的

if(Input::exists()){ 
     $validate = new Validate(); 
     $validation = $validate->check($_POST, array(
      'fullName' => array(
       'required' => true, 
       'min' => 4, 
       'max' => 20 
      ), 
      'username' => array(
       'required' => true, 
       'min' => 4, 
       'max' => 20, 
       'unique' => 'user' 
      ), 
      'email' => array(
       'required' => true, 
       'unique' => 'user' 
      ), 
      'password' => array(
       'required' => true, 
       'min' => 6 
      ), 
      'password_again' => array(
       'required' => true, 
       'matches' => 'password' 
      ) 
     )); 

     if($validation->passed()){ 
      // Register User 

      echo('Passed'); 

     } else { 
      // Output Errors 

      foreach($validation->errors() as $error){ 

       echo $error, '</br>'; 
      } 
     } 
    } 

?> 
<div class="registration"> 
    <h3> Register with WebA<font style="color: #c14a44;">ww</font>ards </h3> 
    <p> Spare us few little seconds, and you'll be glad you did. </p> 
    <form method="post" action=""> 
     <input type="text" id="fullName" value="<?php echo escape(Input::get('fullName')); ?>" name="fullName" placeholder="Your Full name"> 
     <input type="text" id="username" value="<?php echo escape(Input::get('username')); ?>" name="username" placeholder="Choose Username" autocomplete="off"> 
     <input type="email" id="email" value="<?php echo escape(Input::get('email')); ?>" name="email" placeholder="Email address"> 
     <input type="password" id="password" name="password" placeholder="Password"> 
     <input type="password" id="password_again" name="password_again" placeholder="Comfirm password"> 
     <input id="gobutton" type="submit" value="Register"> 

    </form> 

Validate.php

public function check($source, $items = array()){ 

      foreach($items as $item => $rules){ 
       foreach($rules as $rule => $rule_value){ 

        $value = trim($source[$item]); 
        $item = escape($item); 

        // if rule is required and value is empty add error 
        if($rule === 'required' && empty($value)){ 
         $this->addError("{$item} is required"); 
        } else if(!empty($value)) { 
         // DEFINE THE RULES FOR VALIDATION MIN, MAX 
         // USE SWITCH STATEMENT TO SWITCH BETWEEN THE RULES AND CHECK IF VALID 
         // Case for each of the rules defined on the form 
         switch($rule){ 

          case 'min': 

           if(strlen($value) < $rule_value){ 
            $this->addError("{$item} must be a minimum of {$rule_value} characters."); 
           } 

          break; 

          case 'max': 

           if(strlen($value) > $rule_value){ 
            $this->addError("{$item} must be a maximum of {$rule_value} characters."); 
           } 

          break; 

          case 'matches': 

           if($value != $source[$rule_value]){ 
            $this->addError("{$rule_value} must match {$item}"); 
           } 

          break; 

          case 'unique': 

           $check = $this->_db->get($rule_value, array($item, '=', $value)); 

           if($check->count()){ 

            $this->addError("{$item} already exists."); 

           } 

          break; 

         } 
        } 
       } 
      } 

      if(empty($this->_errors)){ 
       $this->_passed = true; 
      } 

      return $this; 
     } 

回答

0

您編寫的代碼將這些錯誤放在您想要的位置。

<?php 

$errors = array(); 

if (!$something) { 
    $errors['field'] = 'Message'; 
} 

?> 

<? if (isset($errors['field'])) : ?> 
    <p class="error"><?php echo $errors['field']; ?></p> 
<?php endif; ?> 
<input type="text" name="field" ...> 
+0

if($ validation-> passed()){ //註冊用戶 echo('Passed'); }其他{ //輸出錯誤 的foreach($ validation->誤差()作爲$錯誤){ 回聲$誤差, '
'; } }我應該擺脫這一點嗎? – Tauciokas

+0

是的。如果您不想在頁面頂部輸出錯誤,請不要在頁面頂部輸出錯誤。所有的驗證都應該在那裏發生,但是你需要將輸出移動到你想要的位置。 – deceze

0

沒有看到更多的代碼,我不能告訴(不包括addError法),但你的驗證類沒有按似乎沒有一種將錯誤與響應中的字段關聯的簡單方法(它只是一個字符串),因此需要做一些工作來完成此操作。我建議將錯誤作爲關聯數組返回,其中鍵是字段的名稱,值是要打印的錯誤消息,並且對於正確的字段留空。然後,在您的註冊文件中,您可以輕鬆引用相關字段。

0

您必須通過錯誤來迭代,而你是輸出領域中,檢查有關這方面的錯誤,並輸出輸入後打印出來,而不是附和上述表單中的所有錯誤。

如果您需要在每個字段中有多個驗證錯誤,那麼errors數組應該是多維的。您應該有一個數組,每個字段名稱都有一個條目。

你還需要這些字段是一個數組來實現這一點。

這樣你可以做這樣的事情(未經測試):

foreach ($field as $f) { 
echo "<input type='{$f->type}' id='{$f->name}' value='{$f->value}' name='{$f->name}' placeholder='{$f->description}'>"; 
if (in_array($f->name, $errors)) { 
echo "<span class='errors'>{$errors[$f->name]}</span>"; 
} 
} 

如果每個字段的錯誤項是一個數組,你就必須附和錯誤,然後來遍歷它。

相關問題