CodeIgniter Forums
Form Generation Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Form Generation Library (/showthread.php?tid=16439)



Form Generation Library - El Forum - 01-01-2010

[eluser]Moru789[/eluser]
Thanks for the help, if I remove the camel-casing it works just fine. That's what you get for copy-pasting javascript when you don't know anything about it :-)


Form Generation Library - El Forum - 01-01-2010

[eluser]Moru789[/eluser]
Is checkbox() supposed to return an array even though I only use one checkbox? Is that something colliding with checkGroup()?


Form Generation Library - El Forum - 01-01-2010

[eluser]BaRzO[/eluser]
You can just use like this...
Code:
->checkbox('test', 'value', 'text', FALSE, 'trim|xss_clean')



Form Generation Library - El Forum - 01-02-2010

[eluser]macigniter[/eluser]
[quote author="Moru789" date="1262375250"]Is checkbox() supposed to return an array even though I only use one checkbox? Is that something colliding with checkGroup()?[/quote]

As a matter of fact it currently does. I will probably change this in future versions. For now only radiobuttons return values as strings. Checkboxes and selects both return arrays simply because I did not want to limit the output since you never know if a form will dynamically add additional elements or not. For example a select could be dynamically changed to a multiple select via javascript and therefore should be able to return the selected items as an array.


Form Generation Library - El Forum - 01-10-2010

[eluser]happydude[/eluser]
How do I change the name of an image before it uploads to the upload_path.

I will like to dynamically assign uploaded images unique filenames.... like md5 of upload timestamp.

how do I do this? ... callback function?


Form Generation Library - El Forum - 01-10-2010

[eluser]hugle[/eluser]
[quote author="happydude" date="1263159122"]How do I change the name of an image before it uploads to the upload_path.

I will like to dynamically assign uploaded images unique filenames.... like md5 of upload timestamp.

how do I do this? ... callback function?[/quote]


Hello!

If I were, I would set upload path to something temporary.
And later, after upload success I would move the file and rename it to specific directory Smile

Good luck!


Form Generation Library - El Forum - 01-10-2010

[eluser]happydude[/eluser]
If the file is big.... that could be resource intensive.

There should be an easier way.


Form Generation Library - El Forum - 01-10-2010

[eluser]Moru789[/eluser]
Moving files doesn't actually move the data so moving a big file is no different from moving a small file (as long as you don't copy and then delete original...)


Form Generation Library - El Forum - 01-12-2010

[eluser]happydude[/eluser]
Yeah, I know. This is not the best code in the world... because I am still a rookie.

But, the concept follow a laid down form generation library structure.

The problem I am having with this is that when I click the submit button regardless of whether the data validates (and updates the database) or does not, it is the old database values that get populated as default values in the fields..... until the next edit of the same row comes up.

Is there any way I can make the newly posted data display as default values in the form.... especially if the form doesn't validate.

Thanks in advance. See the code below:

Code:
function update($id='')
    {
        if ( ! $id ) redirect ('admin/catalog');
        
        $data = $this->product->get($id);
        
        if ( ! $data ) redirect ('admin/catalog');
        
        $this->load->library('form');
        
        $this->data['title'] = 'Add Product';
        $this->data['add_type'] = 'Product';
        
        
        $categories     = $this->admin->prep_for_select('categories');
        $brands            = $this->admin->prep_for_select('brands');
        $sort            = array (1=>1, 2, 3, 4, 5);
        $checked = ($data['featured'] == 1) ? TRUE : FALSE;
        
        $this->form
                 ->open('admin/update/'.$id)
                ->fieldset()
                ->text('name', 'Product Name', 'trim|required|max_length[255]|xss_clean', $data['name'])
                ->text('reference', 'Product Reference', 'trim|required|numeric|xss_clean', $data['reference_id'])
                ->text('quantity', 'Quantity', 'trim|numeric|xss_clean', $data['quantity'])
                ->iupload('userfile', 'Product Image')->br()
                ->select('category', $categories, 'Category', $data['category_id'], 'trim|required|xss_clean|callback__check_cat')
                ->select('brand', $brands, 'Brand', $data['brand_id'], 'trim|required|xss_clean|callback__check_brand')
                ->textarea('description', 'Description', 'trim|xss_clean', $data['description'])->br()
                ->checkbox('featured', '1','  Feature on Home Page?', $checked, 'trim|xss_clean')->br()->br()
                ->select('sort', $sort, 'Sort Order', $data['sort'], 'trim|xss_clean')
                ->span('e.g. A product with a Sort Order of 3 will be placed higher than one with a sort order of 4 in any listing. It also affects the arrangement of featured products in the sidebar.')->br()->br()
                ->submit('Edit Product', 'submit_product')->reset('Clear')
                    ->validate();
                
                    
    
        if ($this->form->valid) {    
            $input = $this->form->get_post();
            
            $added_by = $modified_by = getField('username');
            $date_added = $date_modified = date('Y-m-d H:i:s');
            
            $params = array ( //prep and typecast the data.
                                 'reference_id'     => (int) $input['reference'],
                                 'name'            => (string) $input['name'],
                                 'category_id'    => (int) $input['category'],
                                 'brand_id'        => (int) $input['brand'],
                                 'description'    => (string) $input['description'],
                                 'quantity'        => (int) $input['quantity'],
                                 'featured'        => (int) $input['featured'],
                                 'image'            => (string)$input['userfile']['name'],
                                 'sort'            => (int) $input['sort'],
                                 'added_by'        => $added_by,
                                 'date_added'    => $date_added,
                                 'modified_by'    => $modified_by,
                                 'date_modified'    => $date_modified,                            
                            );
            
            if( ! isset($input['userfile']['name'])) {
                unset ($params['image']);
            }
                            
            $result = $this->product->update($data['id'], $params);
            
            if ($result) {
                $this->data['message'] = '<p class="success">Your product has been successfully edited. You can add another product below, or '.a('products/'.slug($input['name']).'/'.$data['id'], 'View your recently edited product', 'target="_blank"').'.</p>';
                
            } else {
                $this->data['message'] = '<p class="error">There was a problem editing your product. Please try again later.';
            }                
        }
        
        
        $this->data['resource'] = $data['name'];
        $this->data['form'] = $this->form->get();
        $this->data['errors'] = $this->form->errors;
        $this->view = 'admin/add';
        $this->data['update'] = 'TRUE';

N/B Nevermind $this->data. It justs collects data to be sent into the view. I am using Jamie Rumbelow's MY_Controller.


Form Generation Library - El Forum - 01-13-2010

[eluser]hugle[/eluser]
Hello
I think that:
Code:
$this->data['form'] = $this->form->get();
        $this->data['errors'] = $this->form->errors;
should be used, before:

if ($this->form->valid) {