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

(10-27-2015, 12:16 PM)Martin7483 Wrote: What is the url to your form? I'm assuming it is /home and your post url is /home/send. If this is the case then you are defining you rules in the send method and you then validate your posted form.

Define your rules in the home method and do your validation in your home method. Your post URL would then be /home#anchor instead of /home/send. If the validation fails, the view with your form and errors is loaded and the #anchor wil take you to your form. No need for JavaScript.

If the form passed validation you could redirect to your send method and save the post dataset in a flashdata for processing in your send method.


PHP Code:
class Home extends CI_Controller {
 
   
    public 
function home() {
 
       if ($this->form_validation->run() == FALSE) {
 
           $data['post_url'] = '/home#anchor';
 
           $this->load->view('yourform'$data);
 
       } else {
 
           $this->session->set_flashdata('yourform'$this->input->post());
 
           redirect(base_url('home/send'));
 
       }
 
   }
 
   
    public 
function send() {
 
       // If your flashdata item is not set NULL is returned
 
       $post $this->session->flashdata('yourform');
 
       if( ! is_null($post) ) {
 
           // Do what you want to do with your post dataset
 
       } else {
 
           // The form has already been processed
 
           $this->load->view('form_already_processed');
 
       }
 
   }


Maybe my example can help you on your way. In my example the problem of double form posting is also solved
Many thanks for that I can see how it would work when the form is sent. However what happens if you access the home function from, say, a nav button? The validation run happens even though no form data has been sent, it registers 'false', the view is loaded and scrolls down to the form, even though I don't want it to in that case?
Reply
#12

(10-27-2015, 03:30 AM)joseph.dicdican Wrote: Hi,

I would like to share my opinion.

From the thread, I found this block of statement.

Quote:$this->data['body_id'] = 'home';
$this->data['main'] = 'pages/home_view#anchor';    
$this->load->view('templates/template_main_view', $this->data);
The second line causes the error you had. (trying to reach a page .../home#anchor.php which obviously doesn't eist)

I supposed you had a code in templates/template_main_view.php like this

PHP Code:
<!--With some html elements -->
<?
php $this->load->view($main); ?>
$main is not a link it is most likely a string that is referring to a specific file under the View folder. I suggest you can remove the #anchor from your controller method home to remove the error.

Now what I understood, you want to autoscroll to the specific part of your html document and show the errors. 

Quote:To retain your goal to have autoscroll, redirect method will help you. But consequently, you will lose the validation messages since, the page is reloaded.
You can use flashdata for this matter. Storing all the validation messages to flashdata and printing it to your view will solve the problem.

I have searched from the internet and found some helpful links here. 
https://www.codeigniter.com/user_guide/l...#flashdata
http://stackoverflow.com/questions/12353...flash-data

Please refer to my example,

In the Controller,

PHP Code:
public function home($any '') {
 
   $this->session->set_flashdata('error''')
 
   $this->load->view('samples/test');
}

public function 
send() {
 
   $this->load->library('form_validation');

 
   if($this->form_validation->run()) {
 
       //do your stuff here
 
   } else {
 
       $std = new stdClass(); // just and example (to show that flash_data can hold complex values)
 
       $std->error 'some error'
 
       
        
// you can get the validation messages from form_validation
 
       // $std = validation_errors(); //just uncomment this part, this should work
 
       
        $this
->session->set_flashdata('error'$std);
 
   }

 
   redirect(base_url('index.php/sample/home#anchor'));


In View,

Code:
// form above

// The viewport should autoscroll in this part if url has #anchor (any id)
<section id="anchor">
  <?php echo $this->session->flashdata('error')->error; ?>
</section>

I hope I help you somehow. I will make some research on this matter and revise this post if ever I find new approach..

Thank you. Have a nice day.

- joseph.dicdican  Big Grin

Thanks for that. I have used your flashdata solution successfully. The only issue is that the flashdata variable is passed as a string and I want it to be an array so that I can show each error individually. Fortunately this is easily done using a separator parameter of '/n'. Of course I can't use the codeigniter function form_errors() but that isn't a problem. Although this works fine, it may still not be the best solution I suspect.
Reply
#13

(10-28-2015, 02:15 AM)msheath Wrote: Many thanks for that I can see how it would work when the form is sent. However what happens if you access the home function from, say, a nav button? The validation run happens even though no form data has been sent, it registers 'false', the view is loaded and scrolls down to the form, even though I don't want it to in that case?

No it does not scroll down, as accessing /home is not the same as /home#anchor. You only add the #anchor to your post url which you use in your form.
PHP Code:
$data['post_url'] = base_url('/home#anchor');
$this->load->view('yourform'$data); 

Pass the post_url variable as the first argument on form_open
PHP Code:
// in your view
echo form_open($post_url); 

You only add the #anchor to your post URL. Any nav buttons or menu links will link to the page without the #anchor
Reply
#14

(This post was last modified: 10-28-2015, 02:49 AM by joseph.dicdican.)

(10-28-2015, 02:39 AM)msheath Wrote: Thanks for that. I have used your flashdata solution successfully. The only issue is that the flashdata variable is passed as a string and I want it to be an array so that I can show each error individually. Fortunately this is easily done using a separator parameter of '/n'. Of course I can't use the codeigniter function form_errors() but that isn't a problem. Although this works fine, it may still not be the best solution I suspect.

I am glad that helped you somehow. You're welcome. I agree to what you mentioned here,
Quote:Although this works fine, it may still not be the best solution I suspect.
For me, I still need to study about flashdata and whats the use of this in our codeigniter projects. 

For my side I commonly use this kind of codes below to implement error messages reporting/notifications,

Controller part,
PHP Code:

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:

Code:
<?php if($hasError) { // check if has error ?> 
   <section id="errors">
       <?php echo validation_errors(); ?>
   </section>
<?php } //close if has error ?>
We have different style and its up to us how we deal with it. Any style will do as long as their is a result but best practices really matters in the world of programming.  Big Grin

Thank you.

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

(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.

Thanks for that advice. I thought it would be neater to achieve the scrolling result from the controller method without resorting to javascript but if javascript is the best solution, so be it.
Reply
#16

(10-28-2015, 02:54 AM)msheath Wrote:
(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.

Thanks for that advice. I thought it would be neater to achieve the scrolling result from the controller method without resorting to javascript but if javascript is the best solution, so be it.

It's very simple if you think about it ... You want to trigger a scroll event, that's an event in the browser and the browser can only be controller by JavaScript.

The fact that browsers would scroll to a given section within a document after a page is loaded is cool indeed, but you should only use that to give links to other people. Smile
Reply
#17

(10-28-2015, 04:21 AM)Narf Wrote:
(10-28-2015, 02:54 AM)msheath Wrote:
(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.

Thanks for that advice. I thought it would be neater to achieve the scrolling result from the controller method without resorting to javascript but if javascript is the best solution, so be it.

It's very simple if you think about it ... You want to trigger a scroll event, that's an event in the browser and the browser can only be controller by JavaScript.

The fact that browsers would scroll to a given section within a document after a page is loaded is cool indeed, but you should only use that to give links to other people. Smile

That makes total sense - many thanks
Reply
#18

If you want the scrolling effect you can still use my approach and add a scrollTo JavaScript that could be triggered when #anchor is present in the URL. When a user has JavaScript disabled, the browser will still jump to the form because of the #anchor
Reply
#19

(10-28-2015, 05:00 AM)Martin7483 Wrote: If you want the scrolling effect you can still use my approach and add a scrollTo JavaScript that could be triggered when #anchor is present in the URL. When a user has JavaScript disabled, the browser will still jump to the form because of the #anchor

What a fantastic response - thank you for taking the time and trouble to help me here. I get what you are saying and my problem is solved!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB