Welcome Guest, Not a member yet? Register   Sign In
form_textarea() generation issue
#1

[eluser]fdog[/eluser]
I'm using CI form helper to generate the form's inputs. I reuse the same form for inserting and updating. The only difference between the two of them is that the edit-form has the values from the database on the text_inputs (for easy editing).

controller.php:

Code:
/*
   $values is a database row
*/
function _get_form($values = null)
{
    /* form attribute array */
   $form = array(
       'tab_form_attributes' => array(
                        'id' => 'Form',
                        'method' => 'post'
                    ),
                
    /* I use this array to generate the inputs */
    
        'tab_form_list' => array(
            form_label('Name:', 'supplier-name')
        .form_input(
            array(
                'name' => 'supplier-name',
                'id' => 'supplier-name',
                'size' => '30',
                'value' => ( ( $values != null ) ? $values->name : '' ) )), /* prints the value from the database when editing */
                                                                                                            
            form_label('Address:', 'supplier-address')
        .form_textarea(
            array(
            'name' => 'supplier-address',
            'id' => 'supplier-address',
            'rows' => '90',
            'cols' => '12',
            'class' => 'description')),
                    /* Can't use the value attribute in textareas :( */
          ),
                                    
    );
    return $form;
}
In the View.php, I do:
Code:
<?=form_open('suppliers/update_supplier/'.$supplier_id, $tab_form['tab_form_attributes']);?>
    <!-- generate inputs -->
    <?=ol($tab_form['tab_form_list'], array('class' => 'forms'));?>
    <!-- end generate inputs -->    
    
    <--! I create the submit button in a similar way --&gt;
          
&lt;?=form_close();?&gt;

My problem is: since textareas don't use the value attribute how could I change my code to add text from the database to the textarea?
Note: I really like using the form_helper and the ol() function to create forms Tongue
#2

[eluser]Matthieu Fauveau[/eluser]
Hi fdog, form_textarea does in fact support the value attribute.

See :
Code:
if ( ! function_exists('form_textarea'))
{
    function form_textarea($data = '', $value = '', $extra = '')
    {
        $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
    
        if ( ! is_array($data) OR ! isset($data['value']))
        {
            $val = $value;
        }
        else
        {
            $val = $data['value'];
            unset($data['value']); // textareas don't use the value attribute
        }
        
        return "&lt;textarea ".parse_form_attributes($data, $defaults).$extra."&gt;".$val."&lt;/textarea&gt;\n";
    }
}
#3

[eluser]fdog[/eluser]
Thanks Matthiew. They should mention that explicitly in the user guide Undecided
#4

[eluser]Colin Williams[/eluser]
Quote:This function is identical in all respects to the form_input() function above except that it generates a "textarea" type. Note: Instead of the "maxlength" and "size" attributes in the above example, you will instead specify "rows" and "cols".

So, yeah, they sort of do. You just have to reference the form_input() function. I'm not sure they need to explicitly state that the function actually works :-P




Theme © iAndrew 2016 - Forum software by © MyBB