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 - 05-13-2012

[eluser]Agitaous11[/eluser]
Hello,
I am also new to CodeIgniter. And building a webside using CI+FormGenLib

I have to use multiple forms in a page.

My Problem:
I can not stop the code check the unsubmitted form with the other ?automaticly?.
*Errors are different. One's error is not shown under other's error list.

Here is my code to generate required forms using formgenlib under my controller.

Code:
$this->load->library('form');
                $login_form = new $this->form;
                $login_form
                    ->open()
                    ->col(240, 'left', 'left')
                    ->text()
                    ->col(170, 'left', 'left')
                    ->password()
                    ->col(100, 'left', 'right')
                    ->submit('', 'submit_login')
                    ->col(0)
                    ->validate();
                $data['login_form'] = $login_form->get();
                $this->load->view('overal_header', $data);
                $login_form->clear();
                $radio = array(
                    array('', ''),
                    array('', ''),
                );
                $register_form = new $this->form;
                $register_form
                    ->open()
                    ->fieldset()
                    ->text()
                    ->text()
                    .
                    .
                    .
                    ->submit('', 'submit_form');
                    if($this->input->post('submit_form'))
                    {
                        $register_form->validate();
                        if($register_form->valid)
                        {
                            //do the stuff here...
                        }
                    }
                $data['form'] = $register_form->get();
                $data['errors'] = $register_form->errors;
                $register_form->clear();
                $this->load->view('page_welcome', $data);
                $this->load->view('overal_footer');

---
By the way, If I click on first button, //do the stuff doesn't work. But errors are shown.


Form Generation Library - El Forum - 05-13-2012

[eluser]Agitaous11[/eluser]
No one encountered the same problem before?


Form Generation Library - El Forum - 05-13-2012

[eluser]BaRzO[/eluser]
Controller
Code:
/* test */
    function test()
    {

        $this->form
                ->text('Name', 'name', 'required')
                ->submit('Login', 'login');
        $this->form->validate();
        if($this->form->valid)
        {
            echo 'form 1 Valid';
        }
        $data['form1'] = $this->form->get();
        $data['error1'] = $this->form->errors;

        $this->form->clear();

        $this->form
                ->text('Name', 'name', 'required')
                ->submit('Register', 'register');
        $this->form->validate();
        if($this->form->valid)
        {
            echo 'form 2 Valid';
        }
        
        $data['form2'] = $this->form->get();
        $data['error2'] = $this->form->errors;


        $this->load->view('test_view', $data);
    }

// end test
//--------------------------------------------------------

View
Code:
<h2>Login Form</h2>
<h5>Error</h5>
&lt;?=$error1?&gt;
&lt;?=$form1?&gt;

<h2>Register</h2>
<h5>Error</h5>
&lt;?=$error2?&gt;
&lt;?=$form2?&gt;



Form Generation Library - El Forum - 05-13-2012

[eluser]Agitaous11[/eluser]
We found out that, this is a bug with BaRzO. The same code works under v1.0.1 but not v2.0...


Form Generation Library - El Forum - 05-15-2012

[eluser]ckm5[/eluser]
[quote author="Yednotek" date="1336676140"]
From what I saw things get more complicated when you are working with relationships, foreign keys etc. If you have another simple example with relations involved feel free to share Wink In my current project I often have to manage relationships between 5 or 6 tables.

[/quote]

FYI, I'm managing relationships across 20+ tables & two databases, with multiple FK in each table (upto 4), so it's a little more complicated than just a couple of relationships. And one of the DBs has recursive FKs across almost 1m records.

That said, whatever works. There is no 'right' way as long as it does what you need.

Chris.


Form Generation Library - El Forum - 05-15-2012

[eluser]ckm5[/eluser]
[quote author="Agitaous11" date="1336919650"]

I have to use multiple forms in a page.

My Problem:
I can not stop the code check the unsubmitted form with the other ?automaticly?.
*Errors are different. One's error is not shown under other's error list.
[/quote]

I have a multi-page form. Basically, you have to store the results of the first form somewhere. I store it in the session object, which is saved to the db. Not the cookie, which is unsafe and can only store about 4k.

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

  // validate your first form here

  // save your form to the user's session
  $this->session->set_userdata($post);

  // redirect to second form
  redirect('subscription/review');

}

Then I just pull in the old form data as if it was part of the second page form

Code:
function review() {

  // get data from the first form if you need it to create the second form/page
  $session_form = $this->session->all_userdata();

  // create the second form here

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

    // get the data from the first form
  
    $form_data = $this->session->all_userdata();

    // validate second form here

    // save any data you need for thankyou page
    $this->session->set_flashdata($data);
    redirect('subscription/thankyou');

  }
}

Note that your thankyou function should clear out all session data to be safe.

Code:
function thankyou() {
  $ty_msg['welcome_message'] = $this->session->flashdata('welcome_message');
  $this->load->view("thankyou", $ty_msg);
  $this->session->sess_destroy();
}

Works well, but you have to make sure sessions are working properly and that stuff like AJAX calls are not reseting session data.

HTH,

Chris.


Form Generation Library - El Forum - 05-16-2012

[eluser]ckm5[/eluser]
How do you deal with errors after form validation?

I have a form that validates properly, but then needs to check some data against an API.

When the external checks fail, how can I reload the form with errors. Here's what I have now and it doesn't work (form is blank)
Code:
if ($this->form->valid)
  {
   $post = $this->input->post();
    
   $this->load->library('form');     // form creation
  
   $return_data = go_out_to_my_api($post);

   if(!$return_data) {
     $api_error = "There has been an API error";
     $this->form->add_error('name',$api_error);
     $data['form_errors'] = $this->form->errors;
     $this->load->view("review", $data);
   }
  }



Form Generation Library - El Forum - 05-16-2012

[eluser]ckm5[/eluser]
Figured it out. Turns out output buffering was the problem. If you exit in the middle of a code block after calling a view, output buffering will stop it from displaying. You have to echo manually....

[quote author="ckm5" date="1337203605"]How do you deal with errors after form validation?

I have a form that validates properly, but then needs to check some data against an API.

When the external checks fail, how can I reload the form with errors. Here's what I have now and it doesn't work (form is blank)
Code:
if ($this->form->valid)
  {
   $post = $this->input->post();
    
   $this->load->library('form');     // form creation
  
   $return_data = go_out_to_my_api($post);

   if(!$return_data) {
     $api_error = "There has been an API error";
     $this->form->add_error('name',$api_error);
     $data['form_errors'] = $this->form->errors;
     $this->load->view("review", $data);
   }
  }
[/quote]


Form Generation Library - El Forum - 05-17-2012

[eluser]sock[/eluser]
Maybe I'm up too late, or am just having difficulties...

I'm trying to set the config/form.php
Code:
$config['element_suffix']  = '';
dynamically...

Because in some cases, I would like to have a break (
Code:
<br />
) and others I don't, and don't want to have 2 config files to accomplish this.

So I've tried with $this->config->set_item, not working...

I tried doing

Code:
->checkgroup('Example', $example, 'Please select:', $selectedItems, '', array('label_suffix' => '<br />'))
that doesn't work.

This library is very nice and I plan on making a contribution with my first paying job Smile

Any help would be GREATLY appreciated.

Thank you.


Form Generation Library - El Forum - 05-17-2012

[eluser]BaRzO[/eluser]
Use this...
Code:
->checkgroup('Example', $example, 'Please select:', $selectedItems, '')
->br()