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 - 02-23-2011

[eluser]Maglok[/eluser]
Hey macigniter. Thanks for the reply. I did do a clear after building them, instead of after validating.

Unfortunately that isnt working for me. I still get tied to form 1 if form 2 is filled out.

The email field in form 1 is required, but obviously not if form 2 is filled out.

Apologies in advance, but this is the code:
Code:
// Build new user form
        $this->config->load('pona');
        $faculties = $this->config->item('pona_faculties');
        
        $this->form
            ->text('new_email', 'Email address', 'required|valid_email')
            ->select('faculty', $faculties, 'Faculty', '1', 'required')
            ->hidden('type', 'new')
            ->submit('Create account')
            ->add_class('ui-state-default ui-corner-all');

        $data['form_new'] = $this->form->get();

        $this->form->validate();

        // New user form is valid
        if ($this->form->valid) {
            $post = $this->form->get_post();

            // Create new user with email address as login
            $username = $post['new_email'];
            $password = $this->createPassword();
            $additional = array('faculty' => $post['faculty'][0]);
            $group = 'promovendus';
            // Check to see if the username is already registered
            if(!$this->ion_auth->username_check($username)) {
                $this->load->model('model_application', '', TRUE);
                // Register new user
                $application['phd_email'] = $post['new_email'];
                $application['faculty'] = $post['faculty'][0];
                $this->ion_auth->register($username, $password, $username, $additional, $group);
                $this->model_application->add_application($application);
                // Immediately login the user
                $this->ion_auth->login($username, $password, false);
                $this->session->set_flashdata('bericht', 'Account created and logged in.');
                redirect('application/edit', 'refresh');
            } else {
                // User already exists
                $data['errors'] = 'That e-mail address is already registered.';
            }
        }

        $this->form->clear();

        // Build existing user form
        $this->form
            ->text('email', 'Email address', 'required|valid_email')
            ->password('password', 'Password', 'required')
            ->checkbox('remember', '1', 'Remember me', FALSE, 'trim|xss_clean')
            ->hidden('type', 'login')
            ->html('<br/>')
            ->submit('Login')
            ->add_class('ui-state-default ui-corner-all');

        $data['form_login'] = $this->form->get();

        $this->form->validate();

        // Login form is valid
        if ($this->form->valid)    {
            $post = $this->form->get_post(TRUE);
            $remember = $post->remember;

            if($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember)) {
                // Login is successful
                $this->session->set_flashdata('bericht', $this->ion_auth->messages());
                redirect('current', 'refresh');
            } else {
                // Login is unsuccessful
                $this->session->set_flashdata('message', $this->ion_auth->errors());
                redirect('', 'refresh');
            }
        } else {
            $data['errors'] = $this->form->errors;    
        }

I build 2 forms, one at a time. This setup can be found on http://pona.dev.fss.uu.nl Any idea?


Form Generation Library - El Forum - 02-23-2011

[eluser]macigniter[/eluser]
[quote author="Maglok" date="1298477478"]
Unfortunately that isnt working for me. I still get tied to form 1 if form 2 is filled out.
[/quote]

Well, if you use 2 forms on one page you need to give those forms unique names! You need to use

Code:
$this->form->open(current_url(), 'NAME1');



Form Generation Library - El Forum - 02-23-2011

[eluser]Maglok[/eluser]
Can't believe I forgot that. I guess I got lazy since you can ommit the open tag in your library if you just want it set up in default mode.

I have now added: ->open(current_url(), 'new_user')
And another to the other form.

I still get a red circled email block if I am logging in on the other form. No errors actually, which is odd as well.


Form Generation Library - El Forum - 02-23-2011

[eluser]macigniter[/eluser]
[quote author="Maglok" date="1298479788"]
I still get a red circled email block if I am logging in on the other form. No errors actually, which is odd as well.
[/quote]

One more thing should do the trick... use different names for the submit() button like this:
Code:
// form 1
->submit('Create Account', 'signup')

// form 2
->submit('Login', 'signin')



Form Generation Library - El Forum - 02-23-2011

[eluser]Maglok[/eluser]
That did it! Smile

I knew those things in my head. I know submit buttons and forms need unique names. I just never thought to assign them in the library like this. Smile

Thanks! That was a big help.


Form Generation Library - El Forum - 02-23-2011

[eluser]macigniter[/eluser]
[quote author="Maglok" date="1298480618"]I knew those things in my head. I know submit buttons and forms need unique names. I just never thought to assign them in the library like this. Smile[/quote]

The library does a LOT, but not everything :-P

Glad it worked out!


Form Generation Library - El Forum - 02-23-2011

[eluser]c-s-n[/eluser]
Forms do not need a name - but submits should be unique for correct processing, yes


What I just found out after validating my page: If one passes NULL as value to the textarea-function, there is an empty attribute "value" created (which is not valid for this element). It does not happen with an empty string or a boolean value - just with null.

This is in so far a "problem", as I use the data I get from the db-query. And there I have empty fields... At the moment I can live with this validation error, but maybe it's possible that also null can lead to just an empty, valid textarea? I don't want to prepare my whole data... Wink


Form Generation Library - El Forum - 02-23-2011

[eluser]macigniter[/eluser]
[quote author="c-s-n" date="1298489378"]
Forms do not need a name - but submits should be unique for correct processing, yes
[/quote]

Oh, okay. Good :-)

[quote author="c-s-n" date="1298489378"]
What I just found out after validating my page: If one passes NULL as value to the textarea-function, there is an empty attribute "value" created (which is not valid for this element). It does not happen with an empty string or a boolean value - just with null.
[/quote]

Thanks for letting me know! I will change this behaviour so that NULL will not create an empty 'value' attribute. Update will be available this weekend.


Form Generation Library - El Forum - 02-25-2011

[eluser]JonoB[/eluser]
Edit. Nothing to see here. Sorry for my ignorance Wink


Form Generation Library - El Forum - 02-26-2011

[eluser]macigniter[/eluser]
[quote author="JonoB" date="1298433553"]Apologies if this is a silly question: is it possible to get checkbox labels aligned on the left of the checkbox?[/quote]

This is possible with the newest version. Please download the latest version and check the welcome controller. It has an example of how to position the label before the checkbox.