Welcome Guest, Not a member yet? Register   Sign In
Form validation with redirect
#8

(This post was last modified: 10-27-2015, 10:41 AM by joseph.dicdican. Edit Reason: added images for example )

(10-27-2015, 03:42 AM)Narf Wrote: Redirecting is never the solution ... every single time I see a thread with a title similar to this one, I know the OP is doing something wrong.

You can use JavaScript to scroll down in case of an error.

I agree to what Narf said here. I just knew flashdata just this day not deeply. I still don't know yet when to use this feature. It is not a best solution to redirect when implementing notifications/error message reporting.

Regarding the main topic, I would just like to share my usual way on implementing error messages. (e.g. notifications)

First I always have a private property called $data (array)in my controller. (e.g. private $data = array())
Then, I some codes like below,
 Controller part,
PHP Code:
// intended for errors, titles etc.
private $data = array();

public function 
dummy() 
{
 
   $this->send();

 
   $this->load->view('test/test'$this->data);
}

private function 
send() 
{
 
   $this->data['hasError'] = true;
 
   $this->load->library('form_validation');

 
   $this->form_validation->set_rules('username''Username''trim|required');

 
   if($this->form_validation->run()) {
 
       $this->data['hasError'] = false;
 
       //do some stuff here
 
   }


View part,
Code:
<?php if($hasError) { // check if has error ?>
   <section id="errors">
       <?php echo validation_errors(); ?>
   </section>
<?php } //close if has error ?>

For the javascript part to have an autoscroll feature, but I suggest to make use of jquery library to have convenient javascript codings.
Code:
<script type="text/javascript">
   window.onload = function() {
       var errors = document.getElementById('errors');

       //auto scroll only if section#errors exist
       if(errors) { //this also do I assumed, errors.length > 0
           errors.scrollIntoView(true);
       }
   }
</script>
I have tested the auto scroll by adding lots of <br /> element before the section with id errors. It worked fine.
You can also refer to this link to see more of auto scroll using javascript. 

Please see attachment for my local example.
               

I hope this can help. Thank you.

-joseph.dicdican  Smile
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern
Passerelles numériques Philippines | Scholar
[email protected] 
Reply


Messages In This Thread
Form validation with redirect - by msheath - 10-26-2015, 04:42 AM
RE: Form validation with redirect - by Martin7483 - 10-26-2015, 05:34 AM
RE: Form validation with redirect - by msheath - 10-26-2015, 07:26 AM
RE: Form validation with redirect - by Martin7483 - 10-26-2015, 07:44 AM
RE: Form validation with redirect - by msheath - 10-26-2015, 08:09 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:39 AM
RE: Form validation with redirect - by Narf - 10-27-2015, 03:42 AM
RE: Form validation with redirect - by joseph.dicdican - 10-27-2015, 10:29 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:54 AM
RE: Form validation with redirect - by Narf - 10-28-2015, 04:21 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 04:50 AM
RE: Form validation with redirect - by Martin7483 - 10-27-2015, 12:16 PM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:15 AM
RE: Form validation with redirect - by mwhitney - 10-27-2015, 12:29 PM
RE: Form validation with redirect - by Martin7483 - 10-28-2015, 02:41 AM
RE: Form validation with redirect - by Martin7483 - 10-28-2015, 05:00 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 05:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB