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]CtheB[/eluser]
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?


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

[eluser]Mat-Moo[/eluser]
I'm having a blank moment, please someone put me out of my misery!

I'm trying to do a search form, user selects data, once submitted I then run a search, re-display the form with the search (table) underneath...

So there is no onsucess, no model, but I did add a validate. Now the form once send is reporting Valid, and I can do a search, but once the form is regenerated the selected search data is lost. How can I get the search data back in the form...

Code:
$this->form // then we fill the form with elements
            ->open('account/search')
            
            ->fieldset('Search details')
            ->select('fstatusmember',any_array($this->config->item('statusmember')),'Membership type','','')
            
            // correctly finish the form { start }
            ->label(' ','submit')
            ->submit()
            ->reset()
            ->html('</div>')
            ->validate();
        $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
        if ($this->form->valid)
        {
            if(set_value("fstatusmember")<>"") print "OK";
        }
        else
        {
            $this->data["search"]=FALSE;
        }
(So on form submit Ok is shown, but then the form reshown doesn't have the selected data again.


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

[eluser]happydude[/eluser]
Quote:What is the best way to send an email after you’ve filled in the form succesfully, but before the onsucces redirect?
$this->form->submit('send')
->model('form/form_model', 'do_db_stuff')
->onsuccess()

Should you do it in the model?
Or like this?
$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?


Methinks if you wanna use it in another function, just reference it (&formWink just like you would in a model.

But really, I think it makes more sense to use ->validate() and make a $this->form->valid check, send the email within the construct, set a different error message for the email (you can set it in teh flashdata to be displayed).


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

[eluser]seanloving[/eluser]
@Mat-Moo - does this thread address your problem?
http://ellislab.com/forums/viewthread/103956/
-SL


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

[eluser]Mat-Moo[/eluser]
@seanloving : Nope...


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

[eluser]CtheB[/eluser]
i already thought about that, but i don't think that is the best way. Because you will call the validation twice, 1 time in the submit/onsucces, and 1 time for the email.

There must be a better way to do it.

Anyone some ideas about this???

[quote author="happydude" date="1260936335"]
Quote:What is the best way to send an email after you’ve filled in the form succesfully, but before the onsucces redirect?
$this->form->submit('send')
->model('form/form_model', 'do_db_stuff')
->onsuccess()

Should you do it in the model?
Or like this?
$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?


Methinks if you wanna use it in another function, just reference it (&formWink just like you would in a model.

But really, I think it makes more sense to use ->validate() and make a $this->form->valid check, send the email within the construct, set a different error message for the email (you can set it in teh flashdata to be displayed).[/quote]


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

[eluser]seanloving[/eluser]
@Mat-Moo
I tested that this works to preserve the select item after posting.

Code:
->select('fstatusmember', array( 'one' , 'two'), 'Membership type',$this->input->post('fstatusmember'))
Sorry if I still don't understand. -SL


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

[eluser]CtheB[/eluser]
Hi mat-moo,

ofcourse i can try to help you, if you try to help me too and have a look at my topicWink

For your case, its really simple:

Change this code:
Code:
// correctly finish the form { start }
            ->label('&nbsp;','submit')
            ->submit()
            ->reset()
            ->html('</div>')
            ->validate();

Into this:
Code:
// correctly finish the form { start }
            ->label('&nbsp;','submit')
            ->submit()
            ->onsuccess('redirect', 'account/succes');

And add this function to your account controller:
Code:
function succes()
{
           $this->form->validate();

       if($this->form->validated == TRUE)
       {
            $data['post'] = $this->form->get_post();
            $data['form'] = $this->form->get();
        
           $this->load->view('account_succes_view', $data);
       }
           else
           {
              set_error('109', 'The form returned succes, but the validate method has failed');
           }
}

Now you have your form and the data in a view, i think you know yourself how to finish this code nowSmile

you really shouldn't reload the same form, go with an extra 'succes' page...

[quote author="Mat-Moo" date="1260933734"]I'm having a blank moment, please someone put me out of my misery!

I'm trying to do a search form, user selects data, once submitted I then run a search, re-display the form with the search (table) underneath...

So there is no onsucess, no model, but I did add a validate. Now the form once send is reporting Valid, and I can do a search, but once the form is regenerated the selected search data is lost. How can I get the search data back in the form...

Code:
$this->form // then we fill the form with elements
            ->open('account/search')
            
            ->fieldset('Search details')
            ->select('fstatusmember',any_array($this->config->item('statusmember')),'Membership type','','')
            
            // correctly finish the form { start }
            ->label('&nbsp;','submit')
            ->submit()
            ->reset()
            ->html('</div>')
            ->validate();
        $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
        if ($this->form->valid)
        {
            if(set_value("fstatusmember")<>"") print "OK";
        }
        else
        {
            $this->data["search"]=FALSE;
        }
(So on form submit Ok is shown, but then the form reshown doesn't have the selected data again.[/quote]


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

[eluser]macigniter[/eluser]
[quote author="happydude" date="1260915941"]Does Form::get_post() automatically trim and xss_clean data?

I'd like to know because I'll start writing documentation for the remaining parts tommorow.[/quote]

Both: no. I turned off xss_clean due to your &DEV;_0887& problem Smile


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

[eluser]dinhtrung[/eluser]
[quote author="CtheB" date="1260940888"]Hi mat-moo,

Code:
// correctly finish the form { start }
            ->label('&nbsp;','submit')
            ->submit()
            ->onsuccess('redirect', 'account/succes');

And add this function to your account controller:
Code:
function succes()
{
           $this->form->validate();

       if($this->form->validated == TRUE)
       {
            $data['post'] = $this->form->get_post();
            $data['form'] = $this->form->get();
        
           $this->load->view('account_succes_view', $data);
       }
           else
           {
              set_error('109', 'The form returned succes, but the validate method has failed');
           }
}
[/quote]
Is it work? I think the form object in success method and the one activate it won't be the same. Will form elements be preserved if you use redirect as the callback to handle the form data?
@matmoo : Just write another model, or a function in your model, and call it from the main function of your model to handle the form data is enough, I think.