colinbm/kohana-formmanager

Select Option Enum Problems

levinecomputer opened this issue · 6 comments

Created multiple databases with has_many and belongs_to in Models.

However, option returns key for both the key and value.

E.g.:

Class Model_Client extends ORM {
protected $_belongs_to = array ( 'status' => array ( ), );
... table defs for rules() and labels() follow. (Using id for primary key)

and

Class Model_Status extends ORM {
protected $_has_many = array (  'client' => array( ), );
... table defs for rules() and labels() follow. (Using id for primary key)

Select box is automatically created in form, but options are:

<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>

Not what is expected. Should be:

<option value="" selected="selected"></option>
<option value="1">Active</option>
<option value="2">Inactive</option>
etc.

Note: selected="selected" moves appropriately if in edit mode.

Have I set it up wrong? When I tried to force 'display_as', I get 'is_nullable' undefined from Kohana.

You need to set the foreign_name value. E.g.

$this->set_field_value('status', 'foreign_name', 'name');

I'm going by memory, so if that doesn't work let me know, and I'll check when I'm back at the computer.

I tried different permutations. But no success.

If you wouldn’t mind taking a look.

Here are House and Status Models and House and Status Forms. (I removed rules for brevity.)

class Model_House extends ORM {
protected $_belongs_to = array (
     'sex' => array ( ),
     'status' => array ( ),
);
protected $_has_many = array (
     'room' => array ( ),
);

public function rules() {
     return array(
        'id'            => array( ),
        'house'       => array( ),
        'address'    => array( ),
        'city'          => array( ),
        'state'        => array( ),
        'zip'           => array( ),
        'telephone' => array( ),
        'sex_id'      => array( ),
        'status_id'   => array( ),
     );
}

public function labels()
{
    return array(
       'house'       => 'Name',
       'address'    => 'Address',
       'city'          => 'City',
       'state'        => 'State',
       'zip'           => 'Zip Code',
       'telephone' => 'Phone',
       'sex_id'      => 'Male/Female',
       'status_id'   => 'Status',
     );
} }
class Model_Status extends ORM {
     protected $_has_many = array (
        'house' => array( )
        'client' => array( ),
     );

     public function rules() {
        return array(
           'id'            => array( ),
           'status'       => array( ),
        );
     }

     public function labels() {
        return array(
           'id'            => 'Status ID',
           'Status'      => 'Active / Inactive',
        );
     }
}
class Form_House extends FormManager {

protected $model = 'house';

protected function setup() {
     $this->add_field('id', array('display_as' => 'hidden'));
     $this->submit_text = 'Submit';
} }
class Form_Status extends FormManager {

protected $model = 'status';

protected function setup() {
     $this->add_field('id', array('display_as' => 'hidden'));
     $this->submit_text = 'Submit';
} }

The 2 things I notice, is:

  1. The Options not being set for Select (nor for checkboxes and radios à I forced to see what would happen).
  2. The FormManager isn’t picking up my Labels() à is this a function of FormManager??

I really like what you have done here. It will be a great timesaver (along with BootStrap), to producing quick apps.

I would be willing to help write the documentation for these routines.

Thanks,
Toby

Hm, I've just double checked and the code I suggested should definitely work, though it should include the _id suffix. I think you need the following:

class Form_House extends FormManager {

  protected $model = 'house';

  protected function setup() {
    $this->set_field_value('status_id', 'foreign_name', 'status');
  }

}

Note, you don't need to add the id field, and the default value for the submit button is already Submit.

Regarding (2), I appear to have completely missed the labels() method on ORM, so at the moment this isn't used. You can use $this->set_field_value('status_id, 'label', 'Active / Inactive'); however this is definitely something that should be supported. I'll add it to my list or feel free to implement and submit a pull request.

That works ... thanks,

I'll look at enumerating the labels() functionality.

Added ORM labels to _load_model()

Modified lines 136-142 w/

            $table_columns = $this->object->table_columns();
            $table_labels = $this->object->labels();
            foreach ($table_columns as $table_column) {
                if ($table_column['key'] == 'PRI') {
                    $this->primary_key = $table_column['column_name'];
                }
                $this->add_field($table_column['column_name'], $table_column);
                if ( isset($table_labels[$table_column['column_name']]) )
                    $this->set_field_value($table_column['column_name'], 'label', $table_labels[$table_column['column_name']]);
            }

I've added this now. I changed it a bit so it's in its own self contained method, but it's doing the same thing.