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 - 12-15-2009

[eluser]dinhtrung[/eluser]
Just off topic, what is the best way to implement search page? I usually do this:
Code:
/* controller/example */
function index()
{
   $data['form'] = $this->form->open()->text('keyword', "Keyword")
                        ->select('searchcontext', array('username' => 'Username', 'description' => 'Description'), 'Context')
                        ->submit('Search')->model('example_m/do_search')->get();
   $data['table_data'] = $this->db->get('example')->result_array();
   $this->load->view('table', $data);
}
function search()
{
   $searchdata = $this->session->userdata('searchdata');
   $data['form'] = $this->form->open()->text('keyword', "Keyword", '', $searchdata['keyword'])
                        ->select('searchcontext', array('username' => 'Username', 'description' => 'Description'), 'Context', $searchdata['searchcontext'])
                        ->submit('Search')->model('example_m/do_search')->get();
   $data['table_data'] = $this->db->like($searchdata['searchcontext'], $searchdata['keyword'])->get('example')->result_array();
   $this->load->view('table', $data);
}
/* File : models/example_m.php */
   function do_search(&$form, $data)
   {
      $this->session->set_userdata('searchdata', $data);
   }
This is the method I used since I start using CI, so I don't know if it's the best method out there. Any suggestion?


Form Generation Library - El Forum - 12-16-2009

[eluser]macigniter[/eluser]
[quote author="CtheB" date="1260928690"]What is the best way to send an email after you've filled in the form succesfully, but before the onsucces redirect?

Is it possible to add an error to the form in another method in the controller?

Are there any other possibilities to handle the email?

What is the best option to go with?[/quote]

The best way would be NOT to use the onsuccess function, but rather the if ($this->form->valid) clause:

Code:
$this->form
->open('foo')
...
->validate();

if ($this->form->valid)
{
    $this->load->helper('email');
    // do email sending stuff here

    // redirect if everything is okay
    redirect('success');
}

// if the form isn't valid show the form again with errors
$this->data['form'] = $this->form->get(); // this returns the validated form as a string
$this->data['errors'] = $this->form->errors;  // this returns validation errors as a string



Form Generation Library - El Forum - 12-16-2009

[eluser]hugle[/eluser]
[quote author="CtheB" date="1260928690"]What is the best way to send an email after you've filled in the form succesfully, but before the onsucces redirect?

Code:
$this->form->submit('send')
->model('form/form_model', 'do_db_stuff')
->onsuccess()

Should you do it in the model?
Or like this?

Code:
$this->form->submit('send')
->model('form/form_model', 'do_db_stuff');
$this->send_mail();
$this->form->onsuccess();

Is it possible to add an error to the form in another method in the controller?

Are there any other possibilities to handle the email?

What is the best option to go with?[/quote]

I'd do smth like that:
Code:
$this->form->submit('send')
->model('form/form_model', 'do_db_stuff')
->validate();

Actually you could also send mail from inside the model, but I'd do:
if($this->form->valid())
{
//$post = $this->form->get_post();
//$post will containt submited, and validated data. it's nice ! Smile
$this->send_mail($post);
}


hope this helps


Form Generation Library - El Forum - 12-16-2009

[eluser]CtheB[/eluser]
Oke thanx for the help, this is really clear.
Maybe this should be documented better Wink

btw.. i don't need the parameter $post because $this->form->get_post()
will be available in the mail methodSmile


Form Generation Library - El Forum - 12-16-2009

[eluser]macigniter[/eluser]
[quote author="CtheB" date="1260984194"]Maybe this should be documented better Wink[/quote]

Feel free to help :-)


Form Generation Library - El Forum - 12-16-2009

[eluser]happydude[/eluser]
How do I dynamically set the upload folder?

I want images for each method of a controller to be saved in different folders.

uploads to admin/addproduct should go to assets/products
uploads to admin/addcategory should go to assets/categories
uploads to admin/addbrand should go to assets/brands

Any help will be appreciated.


Form Generation Library - El Forum - 12-16-2009

[eluser]Mat-Moo[/eluser]
[quote author="happydude" date="1260991935"]How do I dynamically set the upload folder?

I want images for each method of a controller to be saved in different folders.

uploads to admin/addproduct should go to assets/products
uploads to admin/addcategory should go to assets/categories
uploads to admin/addbrand should go to assets/brands

Any help will be appreciated.[/quote]
Just change the config file
Code:
$config=array(
    'upload_path' => $_SERVER[ 'DOCUMENT_ROOT' ].'/assets/images/upload',
    'allowed_types' => 'gif|jpeg|jpg|png',
    'max_size' => '100'
);
$this->form
    ->upload('fimage','Image','',$config)



Form Generation Library - El Forum - 12-16-2009

[eluser]happydude[/eluser]
I tried that. I got errors about the $config array not getting sent properly into the Form library.

Probably because the config for teh upload path follows this order:

$config['defaults']['upload']['upload_path']

I even tried:

Code:
$config  = $this->config->item('defaults');

$config['upload']['upload_path'] = 'assets/products';

$this->config->set_item('defaults', $config);

$this->load->library('form');

It still did not work.

I need help on this one urgently.


Form Generation Library - El Forum - 12-16-2009

[eluser]hugle[/eluser]
[quote author="happydude" date="1261015740"]I tried that. I got errors about the $config array not getting sent properly into the Form library.

Probably because the config for teh upload path follows this order:

$config['defaults']['upload']['upload_path']

I even tried:

Code:
$config  = $this->config->item('defaults');

$config['upload']['upload_path'] = 'assets/products';

$this->config->set_item('defaults', $config);

$this->load->library('form');

It still did not work.

I need help on this one urgently.[/quote]

I don't understand what are you trying to do, really.

You wanna upload form or what?


Form Generation Library - El Forum - 12-16-2009

[eluser]Mat-Moo[/eluser]
@happydude I tried that. I - got errors about the $config array not getting sent properly into the Form library.
What error did you get exact? That is the correct way to do it, please look at the http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html controller, but with forms you just use ->upload(var,label,config)