扩展Kohana 3.1中的核心类

| 我已经在application / classes / form.php中编写了文件form.php
 <?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) . \' />\';
  }

}

?>
当我使用form :: input时,此函数正在调用,但未在element上应用id属性。 我的代码有什么问题? 使用范例
echo  form::input(\'date\', $cd->year );
o / p
<input type=\"text\" name=\"date\">
    
已邀请:
尝试过您的代码,它按预期工作。仔细检查
$value
参数(在您的情况下为
$cd->year
)不是
NULL
HTML::attributes()
将跳过具有
NULL
值的属性;您的自定义输入法会添加一个等于value的id,因此,如果value为
NULL
,则id也会太大,并且不会呈现为属性。     
尝试这个;
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);
    }

}
    

要回复问题请先登录注册