CodeIgniter - Form processing is slow, and doesn't return values on error
I'm trying to condense my controller a bit, mainly due to URI aesthetics. Right now my controller works, but if there's an error I'm left with controller/process which I don't really want to be there, I'd rather it be add.
So I attempted to rebuild my controller with
public function add()
{
if ( $this->input->server('REQUEST_METHOD') === 'POST' )
{
if ( $this->form_validation->run('add') === FALSE )
{
$this->add(); // reload the method/form for errors.
}
else
{
$this->_process(); // private function for inserting the data.
}
}
else
{
$this->load->view('add_form')
}
开发者_如何学运维}
What happens is the form takes a dreadful long time (only like 5 seconds, but that's still disgustingly horrible), then reloads the form, but no errors are displayed and forms are not repopulating.
I'm feeling like this is a super simple issue that's pure user error, but I can't pick out exactly what I'm doing wrong here that's making this process be so finicky.
When the form fails to load, you are calling the method add()
again which is probably causing it to keep looping through itself. I would refactor the code to something like this:
public function add()
{
if ( $this->form_validation->run('add') )
{
$this->_process(); // private function for inserting the data.
redirect('controller_name/method_name');
}
$this->load->view('add_form')
}
Notice I removed the check to see if there is a POST sent because the form won't validate if there is no $_POST
var to check it against. Also, I put in a redirect()
to send the user to a different method, or even reload the same method, but it will clear the $_POST
and stop the rest of the method from executing.
精彩评论