Php 在Kohana 3.1中扩展核心类

Php 在Kohana 3.1中扩展核心类,php,kohana,Php,Kohana,我已经在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 n

我已经在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) . ' />';
  }

}

?>
o/p


尝试了您的代码,它按预期工作。仔细检查
$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);
    }

}

作为提示,您可以随时调用父函数,这样就不会重复。谢谢,我修改了我的函数,使之具有父输入调用。
<input type="text" name="date">
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);
    }

}