Form validation with redirect |
(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.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? (10-27-2015, 03:30 AM)joseph.dicdican Wrote: Hi, 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.
(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'); Pass the post_url variable as the first argument on form_open PHP Code: // in your view You only add the #anchor to your post URL. Any nav buttons or menu links will link to the page without the #anchor
(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. Code: Code: <?php if($hasError) { // check if has error ?> Thank you. -joseph.dicdican
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern Passerelles numériques Philippines | Scholar [email protected] (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. 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. (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. 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. (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. That makes total sense - many thanks
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
(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! |
Welcome Guest, Not a member yet? Register Sign In |