Vivek Goel
Vivek Goel

Reputation: 24140

Extending core class in Kohana 3.1

I have written file form.php in application/classes/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) . ' />';
  }

}

?>

when I used form::input this function is calling but it is not applying id attribute on the element . what is wrong in my code ?

Usage example

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

o/p

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

Upvotes: 1

Views: 740

Answers (2)

nickg
nickg

Reputation: 11

Try this;

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);
    }

}

Upvotes: 1

David Hancock
David Hancock

Reputation: 14991

Tried your code and it worked as expected. Double check that the $value argument ($cd->year in your case) isn't NULL.

HTML::attributes() will skip attributes that have a NULL value; your custom input method adds an id equal to value, so if value is NULL id will be too and it will not be rendered as an attribute.

Upvotes: 2

Related Questions