2012-01-15 74 views
0

我想在窗體上顯示錯誤,突出顯示有錯誤的字段,並在字段旁邊顯示錯誤文本。如果沒有優雅的方式來顯示每個字段旁邊,上面會很好。Kohana 3.2,在窗體中顯示錯誤

我發現早期版本的例子,但API似乎已經改變,它們不適用於3.2。

這只是一個我正在學習Kohana的項目,所以並不重要。我只想知道處理這個問題的「kohana」方式。

在我的控制,我有這樣的:

if (isset($_POST) && Valid::not_empty($_POST)) 
{ 
    $post = Validation::factory($_POST) 
    ->rule('zipcode', 'not_empty')); 

    if ($post->check()) { 
     $errors = $post->errors('zipcode'); 
    } 
} 

$this->template->content = View::factory('myview', $data) 
->bind('errors', $errors); 

這裏是我的形式「myview.php」:

<?php echo Form::open(); ?> 
<dl> 
    <dt><?php echo Form::label('zipcode', 'Zip Code') ?></dt> 
    <dd><?php echo Form::input('zipcode') ?></dd> 
</dl> 
<p><?php echo Form::submit(NULL, 'Get Records'); ?></p> 
<?php echo Form::close(); ?> 

回答

3

我已經採取了擴展窗體幫助程序類的方法,以在窗體字段上添加「錯誤」類名稱,並在字段標籤中顯示錯誤消息。

<?php defined('SYSPATH') or die('No direct script access.'); 

class Form extends Kohana_Form { 

    private static function attributes($name, & $attributes = NULL, $errors = NULL) 
    { 
     // Set the id attribute 
     if (!isset($attributes['id'])) 
     { 
      $attributes['id'] = $name; 
     } 

     if ($errors !== NULL) 
     { 
      // Merge in external validation errors. 
      $errors = array_merge($errors, (isset($errors['_external']) ? $errors['_external'] : array())); 

      // Set the error classname 
      if (isset($errors[$name])) 
      { 
       $attributes['class'] = trim((string) @$attributes['class'].' error-field');    
      } 
     } 
    } 

    public static function input($name, $value = NULL, array $attributes = NULL, array $errors = NULL) 
    { 
     static::attributes($name, $attributes, $errors); 

     return parent::input($name, $value, $attributes); 
    } 

    public static function select($name, array $options = NULL, $selected = NULL, array $attributes = NULL, array $errors = NULL) 
    { 
     static::attributes($name, $attributes, $errors); 

     return parent::select($name, $options, $selected, $attributes); 
    } 

    public static function password($name, $value = NULL, array $attributes = NULL, array $errors = NULL) 
    { 
     static::attributes($name, $attributes, $errors); 

     return parent::password($name, $value, $attributes); 
    } 

    public static function textarea($name, $body = '', array $attributes = NULL, $double_encode = TRUE, array $errors = NULL) 
    { 
     static::attributes($name, $attributes, $errors); 

     return parent::textarea($name, $body, $attributes, $double_encode); 
    } 

    public static function file($name, array $attributes = NULL, array $errors = NULL) 
    { 
     static::attributes($name, $attributes, $errors); 

     return parent::file($name, $attributes); 
    } 

    public static function label($input, $text = NULL, array $attributes = NULL, array $errors = NULL, $view = 'messages/label_error') 
    { 
     if ($errors !== NULL) 
     { 
      // Merge in external validation errors. 
      $errors = array_merge($errors, (isset($errors['_external']) ? $errors['_external'] : array())); 

      // Use the label_error view to append an error message to the label 
      if (isset($errors[$input])) 
      { 
       $text .= View::factory($view)->bind('error', $errors[$input]); 
      } 
     } 

     return parent::label($input, $text, $attributes); 
    }  
} 

然後通過$errors數組的標籤和字段的輔助方法中:

<?php echo 
    Form::label('username', 'Username', NULL, $errors), 
    Form::input('username', $user->username, NULL, $errors); 
?> 

這個想法被建議在Kohana的論壇,但我一直在努力尋找原來的線程。無論如何,我發現這種方法最適合我。

[編輯]查看這種方法的一個例子在這裏的行動:http://kohana3.badsyntax.co/contact(提交表單)

+0

謝謝,這是我一直在尋找的功能。好的解決方案,應該內置。這也是一個很棒的小演示網站。那不是可以下載的嗎? – Coder1 2012-01-15 16:19:17

+1

歡迎在這裏看看:https://github.com/badsyntax/kohana3-examples(這基本上是演示網站後面的代碼),但我不建議嘗試下載並使其運行爲事情已經破裂。代碼是相當古老的,所以把所有東西都用一小撮鹽..在很多情況下,我會以不同的方式做現在的事情。 – badsyntax 2012-01-15 16:34:58

+0

如果我可以爲後期綁定,很好地使用通過引用,@ @屬性和一些寫得很好的github上的參考代碼。非常感謝!如果您可以更新代碼庫以使其成爲最新版本,那將是甜蜜的。 – aleemb 2012-05-02 20:24:43

0

這是我用於個人實驗一些示例代碼與Kohana形式。它是聯繫表單的一部分。這應該適合你。

下面的代碼顯示了一個聯繫表單。用戶提交表單後,會給出反饋(失敗+錯誤/成功)。

if (isset($errors) && count($errors) > 0) 
{ 
    echo '<ul>'; 

    foreach ($errors as $error) 
    { 
     echo '<li>' . $error . '</li>'; 
    } 

    echo '</ul>'; 
} 
// form 
echo Form::open(null); 

    // fields 
    echo Form::label('firstname') . Form::input('firstname', null, array('id' => 'firstname')) . '<br />'; 
    echo Form::label('email') . Form::input('email', null, array('id' => 'email')) . '<br />'; 
    echo Form::label('message') . Form::textarea('message', '', array('id' => 'message')) . '<br />'; 

    // submit 
    echo Form::submit('submit', 'Send message'); 

echo Form::close(); 

在控制器中,我驗證窗體並將錯誤消息和成功消息分配給視圖。

public function action_index() 
{ 
    // make view 
    $view = View::factory('pages/contact') 
     ->bind('post', $post) 
     ->bind('errors', $errors) 
     ->bind('success', $success); 

    // check if form is submitted 
    if ($_POST) 
    { 
     // trim fields 
     $post = array_map('trim', $_POST); 

     $post = Validation::factory($_POST) 
      ->rules('firstname', array(
       array('not_empty'), 
       array('min_length', array(':value', '2')) 
      )) 
      ->rules('email', array(
       array('not_empty'), 
       array('email') 
      )) 
      ->rule('message', 'not_empty'); 

     if ($post->check()) 
     { 
      $success[] = 'Thank you for your message!'; 
     } 
     else 
     { 
      $errors = $post->errors('contact'); 
     } 
    } 

    // view 
    $this->response->body($view); 
} 

我希望這有助於!

相關問題