Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Fieldsets and ORM
  • Hi, I've created a model and setup it's properties. Some of them are selects. However, when I call Fieldset::factory('usuario')->add_model($usuario)->build() the generated form shows all fields as text inputs. Is this a bug in the Fieldset?
    class Model_Usuario extends Orm\Model {
        protected static $_table_name = 'usuarios';
        
       protected static $_properties = array(
            'id' => array('type' => 'int'),
            'username' => array(
                'type' => 'varchar',
                'label' => 'Nome completo',
                'validation' => array('required', 'min_length' => array(8), 'max_length' => array(64))
            ),
            'email' => array(
                'type' => 'varchar',
                'label' => 'E-mail',
                'validation' => array('required', 'valid_email')
            ),
           'status' => array('type' => 'select',
                            'label' => 'Status',
                            'options' => array('0' => 'Inativo',
                                                '1' => 'Usuário',
                                                '2' => 'Administrador',
                                                '3' => 'Super Admin'))
    
        );
    

    Form generation:
    Fieldset::factory('usuario')->add_model('Model_Usuario')->build();
    

    Status should be a select, but is rendered as text input.
  • 'type' and 'options' need to be under a key named 'form' and that's only supported as of 1.1 (which is still in development). You put in 'varchar' for 'username' but that's a data_type and not a 'type' (which is text/textarea/select/radio/checkbox).
    class Model_Usuario extends Orm\Model {
        protected static $_table_name = 'usuarios';
        
       protected static $_properties = array(
            'id' => array('data_type' => 'int'),
            'username' => array(
                'data_type' => 'varchar',
                'label' => 'Nome completo',
                'validation' => array('required', 'min_length' => array(8), 'max_length' => array(64)),
                'form' => array('type' => 'text'),
            ),
            'email' => array(
                'label' => 'E-mail',
                'validation' => array('required', 'valid_email'),
                'form' => array('type' => 'varchar'),
            ),
           'status' => array(
                            'label' => 'Status',
                            'form' => array(
                                  'type' => 'select',
                                  'options' => array('0' => 'Inativo',
                                                '1' => 'Usuário',
                                                '2' => 'Administrador',
                                                '3' => 'Super Admin')
                                  ),
                            ),
        );
    
  • Jelmer, Thanks for the reply. That makes sense. I'll move form types to a specific array in the model properties and tweak the Observer_Validation::set_fields method to read from it. I've also extended the Fieldset class to implement a remove method, so it's easier to remove just a few properties (like 'id'):
     public function remove($fields)
     {
                if (is_array($fields)) {
                    foreach ($fields as $field) {
                        if (array_key_exists($field, $this->fields))
                                unset($this->fields[$field]);
                    } 
                
                    return $this;
                }
                
                if (array_key_exists($fields, $this->fields))
                    unset($this->fields[$fields]);
    
                return $this;
     }
    

    This allows something like
    echo Fieldset::factory('article')->add_model($article)->remove('id)->build();
    

    I don't know if there's something like that for 1.1 or if there's a better way to implement it. But it helps a lot. Thanks again, and congratulations for the nice work!
  • Yeah, it did the trick! In the Observer_Validation::set_fields method just changed
    $field = $fieldset->add($p, ! empty($settings['label']) ? $settings['label'] : $p);
    
    to:
    $field = $fieldset->add($p, ! empty($settings['label']) ? $settings['label'] : $p, 
    ! empty($settings['form']) ? $settings['form'] : array());
    

    The text input is the default, so only different input types must be setup for the forms. Just added this to my model:
           'status' => array('type' => 'int', //<== the data type
                            'label' => 'Status',
                            'form' => array(
                                'type' => 'select',  //<== the form type                      
                                'options' => array('0' => 'Inativo',
                                                    '1' => 'Usuário',
                                                    '2' => 'Administrador',
                                                    '3' => 'Super Admin')))
    

    And now this works perfectly:
    echo Fieldset::factory('usuario')->add_model('Model_Usuario')->remove('id')->build();
    

    Just great! :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion