2011-04-13 156 views
1

我已經在應用/類書面文件form.php的/ form.php的擴展核心類3.1

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

class Form extends Kohana_Form { 

    public static function input($name, $value = NULL, array $attributes = NULL) { 
    // Set the input name 
    $attributes['name'] = $name; 
    // Set the input value 
    $attributes['value'] = $value; 
    if (!isset($attributes['id'])) { 
     $attributes['id']= $value; 
    } 
    if (!isset($attributes['type'])) { 
     // Default type is text 
     $attributes['type'] = 'text'; 
    }  
    return '<input' . HTML::attributes($attributes) . ' />'; 
    } 

} 

?> 

,當我使用的形式::輸入這個功能調用,但它並不適用id屬性上元素。 我的代碼有什麼問題?

用法舉例

echo form::input('date', $cd->year); 

O/P

<input type="text" name="date"> 
+0

作爲提示,您可以隨時調用父功能,以免重複。 – 2011-04-13 16:39:36

+0

謝謝我修改了我的函數以便有父輸入調用。 – 2011-04-14 04:35:00

回答

2

想你的代碼和它的工作如預期。請仔細檢查$value參數(您的案例中的$cd->year)不是NULL

HTML::attributes()將跳過具有NULL值的屬性;您的自定義輸入法會添加一個等於值的ID,因此如果值爲NULL,則ID將會過大,並且不會將其顯示爲屬性。

1

試試這個;

class Form extends Kohana_Form { 

    public static function input($name, $value = NULL, array $attributes = NULL) 
    { 
     if (empty($attributes['id'])) 
     { 
      $attributes['id']= $name; // not value 
     } 

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

}