Hi,
I have some questions about EE development that I am hoping someone can help with. I have never used EE before the public beta of 2.0, but I do have some familiarity with CI and I am having some problems combining the two. In CI, my process for handling form submission was something like this:
) In a controller, check if any data has been POSTed.
) If so, do form_validation->run() and return the form with error messages if there are errors.
) If there was no POST data, just display the form. The form action submitted to its own URL, so I knew the form would always end up back at this controller.
) The form was stored in its own View file, separate from the content of the page.
) As an extra bonus: If the form was submitted via AJAX I would just return the HTML of the form instead of the whole page. If if was a non-AJAX request I would load the Views required to build the entire page.
I am having some problems coming up with a method of achieving this simplicity in EE. My usual method of learning my way around an open-source project is to look through the source and see how the problem has been solved elsewhere, but as I only have the trial version of EE 2.0 this is not an option until I buy a license (which I will do as soon as I can be sure I can achieve this functionality… a bit of a Catch 22 situation).
Here’s what I have tried in EE:
) Created a module called testme with a function “test_function”:
function test_function()
{
$foo = $this->EE->input->post('foo');
$this->EE->load->library('form_validation');
$this->EE->output->enable_profiler(TRUE);
$this->EE->form_validation->set_old_value('foo',$foo);
$this->EE->form_validation->set_rules('foo', 'Test Field', 'required');
if ($this->EE->form_validation->run() == FALSE)
{
print "nope";
//I also tried:
//return "nope";
// and:
//$this->return_value = "nope";
//return true;
}
else
{
print "yup";
}
return true;
}
) Created a template “home/index” which contains the following code:
<form action="http://192.168.1.4/ee/index.php/testgroup/page" method="post">
<input type=hidden name=ACT value="21"> //hard-coded while I am testing
<input type=text name=foo value="test">
<input type=submit>) Action ID 21 corresponds to “Testme::test_function” (confirmed in phpMyAdmin)
) Created a template “testgroup/page” which at the moment contains the text “this is some content”
Now the validation works - if my field contains a value I see the correct response. But I do not see any other content. It is almost as if processing stops after my function completes. I know I am just printing out a string and not returning anything, but I am not quite sure what I should be returning.
The Expression Engine + CodeIgniter says:
‘Once you have your module set up, it basically acts as a mini CodeIgniter “application” folder similar to a regular CodeIgniter installation. You can create a /view folder to hold your view files, and a /models folder to hold your models. Note: The view files will be used in the Control Panel *only*. Your EE templates act as your front-end “views”.’
As this is for a front-end form I know I need to use the EE template methods, but I can’t find any info on how to do this.
I would be grateful if someone could help me answer the following questions. I have searched the forums, wiki and docs but can not find the answer. I’m new to EE though and may be missing something really basic, so feel free to reply with “RTFM: $useful_link” 😊
) Am I on the right track here? From reading the docs, I think this is how forms should be submitted. But I don’t see any info on how to actually continue the validation process by returning the form to the user. Any recommended links to help me get my head around this?
) How do I pass control back to EE after performing validation, so that the form (with error messages) can be displayed to the user, in the correct style for its template?
) Is it possible to use CI views to load a section of the page (in this case a form) and add my validation error messages before returning that to the user?
) I understand that I can use tags to get strings from modules ({exp:testme:test_output} {foo} {/exp:testme:test_output}. Is there some way I could use this in a template like: {exp:testme:process_form} {form_contents} {/exp:testme:process_form}, where the process_form function processes POST data (if any), then populates form_contents with the form’s HTML?
If I can answer these questions I will be a very happy man, especially if the last two are possible. I can then use a simple tag in the template and store validation rules in the module, and the form HTML in View file.
Thanks,
Mike