CodeIgniter Forums

Full Version: Form Library - No Fun
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Kyle Johnson[/eluser]
Hi everyone,

I've been using codeigniter for about 2 months now, my first experience with MVC setups. I like the idea of using the same pages multiple times, it makes everything really simple to develop.

I'm having issues with this form library.

I have a number of forms that I want to be able to create dynamically, but they have complicated (to me) instructions.

For example, I am creating a from that has a few static pieces of information at the top, and then has 'n' line items, but the drop downs won't generate until other drop downs have been filled in. Requiring a reload on every 'onchange' event.

I just feel there is a better approach to this.

I am loading the Form library and generating all the data to pass to the form in the controller. The $data variable is then passed to the view and I echo the $data['form'] variable using echo $form.

I want to also somehow tweak the Form library here to use validation, but I'm not sure if it's possible.

The form library isn't finished being updated, so I am building on to it as I go, but maybe someone can point me in a better direction?

El Forum

[eluser]Kyle Johnson[/eluser]
I've been browsing a bit in the forums, and it seems I'm not the only one with FORM issues.

It's difficult to create dynamic, user-friendly forms. I could give out data input fields for every field all day, but I think the users would prefer to have drop downs and see data only pertinent to them.

How do application developers manage this amount of work? I love working on databases and designing the logic for things, but when it comes to writing out the GUI code.. it just becomes tedious. Is that just me?

El Forum

[eluser]slowgary[/eluser]
I haven't checked out your .zip yet but if you have a few dynamic dropdowns and you want to autopopulate them as the user selects (sort of 'drilling down'), I would use JavaScript to fill in the select options onchange. So you'd just echo a multidimensional array, then let JavaScript do the rest. I'll check out your .zip tomorrow and see if I can prototype something.

El Forum

[eluser]Kyle Johnson[/eluser]
I've seen the JavaScript method done a couple ways...

AJAX method, pulling the data after something is chosen.
Simpler method, pulling all the data and storing it in a javascript variable.

I'm a bit weary to do either because we have some people that use the page with their mobile phones. (There will eventually be a /mobile/ section). I've seen a couple that won't even handle a "this.form.submit();" call. Then again, this is all relatively new to me, so I appreciate any tips you can provide.

I want to go with javascript to pull the data dynamically based on what other menus are chosen, as that would ease the load on the server (since on reload, it would run the queries for the other drop downs again anyways).

Thanks!

El Forum

[eluser]slowgary[/eluser]
A lot of mobile phones can handle JavaScript now. Obviously you'll always be counting someone out. Then again, this won't work for Lynx users either. Smile I played around with something, but my brain is tired. It would be easy to do if you always know how many selects you'd need, but it'd be much better if you could process it recursively, allowing an unlimited number of levels.

El Forum

[eluser]Kyle Johnson[/eluser]
With javascript you mean??

Right now I have it setup (I'm not even sure if it's in the code I posted)...

I have it setup to have it take these steps (via form.submit()Wink

Select customer (where user has permission to that customer)
if customer selected then
(
select customer locations
select approved ship methods (assuming no override)
select customer part listing
)

I know what you mean about being tired. I've been drained the past few days trying to push this project further and further. It's exciting and fun, but once I get to this part, creating the forms I just die.

El Forum

[eluser]TheFuzzy0ne[/eluser]
You can use validation with no adjustments necessary - You just need to show all errors in a single page block. The question really, is where do you want to display your errors? If you can show me how you want the mark-up to be, I could probably suggest a function that should work for you.

El Forum

[eluser]TheFuzzy0ne[/eluser]
Just an observation - The __toString() method might be better off using is_array(), rather than isset() and empty(). I've also shuffled it about slightly to save a level of indentation (this is just my personal perference).

Code:
// magic method actually used to display the form defined
public function __toString() {
    $chaineTemp = '';
    if ( ! isset ($this -> formBuffer['open']) || ! isset ($this -> formBuffer['close'])) {
        return '';
    }
    $chaineTemp = $this -> formBuffer['open'];
    if (is_array ($this -> formBuffer['elements'])) # No point doing looping if it's not an array.
        foreach ($this -> formBuffer['elements'] as $clef => $val) {
            if (isset ($this -> formBuffer['anything'][$clef])) {
                $chaineTemp .= $this -> formBuffer['anything'][$clef];
            }
            $chaineTemp .= $val;
        }
    }
    $chaineTemp .= $this -> formBuffer['close'];

    return $chaineTemp;
    }

Just my two cents.

El Forum

[eluser]Kyle Johnson[/eluser]
I will definitely take the suggestion into account.

The library is the work of someone else (no name on it) that was posted on one of the PHP script pages.

I just don't think it's possible to use the set_value() function with any of the current modules out there.

My biggest slowdown is my requirement for array form objects. For example:

Code:
<input type="text" name="option[]" value="" size="50" />
<input type="text" name="option[]" value="" size="50" />
<input type="text" name="option[]" value="" size="50" />

<label for="multi">Multiple Select</label>
<select name="single" id="multi" multiple="multiple" style="height:150px">
<optgroup label="Group X">
   <option value="1">One</option>
   <option value="2" selected="selected">Two</option>
   <option value="3">Three</option>
</optgroup>
<optgroup label="Group Y">
   <option value="4">Four</option>
   <option value="5" selected="selected">Five</option>
</optgroup>
</select>

It took some work to get the set_value() function to support repopulating the 'option[]' array, even though in the 1.7.1 user_guide it says it supports it. It just repopulated it with "Array", so it was close, but didn't work, which also meant the validation_error didn't work.

I have a few issues generating dropdown menus for these. Let me show you a current form that is being used.

El Forum

[eluser]Kyle Johnson[/eluser]
This is in the view
Code:
function ship()
    {
        $data['title'] = 'iTEK Services, Inc. - Service Inventory Database - Welcome';
        $data['subheading'] = 'Ship Something Good.';
        
        $this->db->select("company_id, concat(company_name,' (', group_name,')') AS c_name", FALSE);
        $this->db->from('company');
        $query = $this->db->get();
        
        $fdata['company_id'][0] = '---Choose One---';
        foreach($query->result_object() as $val)
        {
            $fdata['company_id'][$val->company_id] = $val->c_name;
        }
        
        
        $fdata['to_location_id'][0] = '---Choose One---';
        if($this->input->post('company_id'))
        {
            $this->db->select("location_id, location_code, group_name");
            $this->db->from('location');
            $this->db->join('company', 'company.company_id = location.company_id');
            $this->db->where('location.company_id', $this->input->post('company_id'));
            $this->db->order_by('group_name asc, location_code asc');
            $query = $this->db->get();
            foreach($query->result_object() as $val)
            {
                $fdata['to_location_id'][$val->location_id] = $val->location_code;
            }
        }
        
        $this->db->select("location_id, location_code");
        $this->db->from('location');
        $this->db->where('location.company_id = 2');
        $this->db->order_by('location_code asc');
        $query = $this->db->get();
        
        foreach($query->result_object() as $val)
        {
            $fdata['from_location_id'][$val->location_id] = $val->location_code;
        }

        /*
         * SELECT c.Company_MasterModel_id, CONCAT(IFNULL(c.ShortDesc,m.Description),' (',c.PartName,')') AS listname
            FROM company_mastermodel AS c INNER JOIN mastermodel AS m ON c.MasterModel_id = m.MasterModel_id
            WHERE (((c.Company_id)=%d))
            ORDER BY (CASE WHEN c.ShortDesc IS NULL THEN 1 ELSE 0 END ), c.ShortDesc, m.Description
         */
        
        $fdata['company_master_model_id'][0] = '---Choose One---';
        if($this->input->post('company_id'))
        {
            $this->db->select("c.company_master_model_id, CONCAT(IFNULL(c.short_description,m.description),' (',c.part_name,')') AS list_name", FALSE);
            $this->db->from('company_master_model AS c');
            $this->db->join('master_model AS m', 'c.master_model_id = m.master_model_id');
            $this->db->where('c.company_id', $this->input->post('company_id'));
            $query = $this->db->get();
            
            foreach($query->result_object() as $val)
            {
                $fdata['company_master_model_id'][$val->company_master_model_id] = $val->list_name;
            }            
        }


        $selArr = array('onchange'=>"form.refresh.value='1'; form.submit();");

        // create form
        $form = $this->form;

        $form -> open_form(array ('action' => ''));
        
        // hidden fields
        $form->add_input('hidden', array('name'=>'refresh'));
        // end hidden fields
        
        $form->open_fieldset();
        $form->add_legend('Package Information');
        $form->add_label('Customer ID');
        $form->gen_select('company_id', $selArr, $fdata['company_id'], $this->input->post('company_id'));
        $form->add_anything('<br /><br />');
        $form->add_label('Ship To Location', array('for' => 'to_location_id', 'title'=>'Where the package is going to.'));
        $form->gen_select('to_location_id', $selArr, $fdata['to_location_id'], $this->input->post('to_location_id'));
        $form->add_anything('<br /><br />');
        $form->add_label('Ship From Location', array('for' => 'to_location_id', 'title'=>'Where the package is coming from.'));
        $form->gen_select('from_location_id', $selArr, $fdata['from_location_id'], $this->input->post('from_location_id'));
        $form->add_anything('<br /><br />');
        $form->add_label('Tracking Number');
        $form->add_input('text', array('name'=>'tracking_number', 'value'=>$this->input->post('tracking_number')));
        $form->close_fieldset();
        
        $form->open_fieldset();
        $form->add_legend('Package Contents');
        if($this->input->post('gen_rows'))
        {
            $gen = $this->input->post('gen_rows');
            if($gen>100)
                $gen=100;
        } else {
            $gen = 1;
        }
        $form->add_label('Number of Lines to Display');
        $form->add_input('text', array('name'=>'gen_rows','value'=>$gen, 'onchange'=>"form.refresh.value='1'; form.submit();"));
        $form->add_label('(Updates when you click outside of cell)');
        $form->add_anything('<br />Model | Quantity | Serial | Asset<br />');
        for($i=0; $i<$gen; $i++)
        {
            $form->gen_select('company_master_model_id[]', $selArr, $fdata['company_master_model_id'], $this->input->post('company_master_model_id[]'));
            $form->add_input('text', array('name'=>'quantity[]'));
            $form->add_input('text', array('name'=>'serial[]'));
            $form->add_input('text', array('name'=>'asset[]'));
            $form->add_anything('<br />');
        }
        $form->close_fieldset();
        
        $form->add_input('submit', array('name'=>'realsubmit', 'value'=>'Create Shipment'));
        $form->add_input('reset');
        $form->close_Form();
        
        $data['form'] = $form->output();
        
        $this->load->view('header', $data);
        $this->load->view('subsystem/shp/index',$data);
        $this->load->view('footer', $data);
    }
Pages: 1 2