Welcome Guest, Not a member yet? Register   Sign In
Form validation hell !, with more than one form on a single view
#1

[eluser]edjon2000[/eluser]
Hi all Smile

Sadly I have another set of newbie questions.

The Problem:
I have been trying to make sense of the form validation class, and how to apply it to a situation where there is more than one form in a view, but, I am running into the problem where, if I submit one form, validation fails on the other.

What I have done so far
I have spent the last week or so looking around these fine forums to try and find a demonstratable version I can adapt but, whilst I have found a few partial solutions, they do not go into enough depth as to how to apply them.

My Questions
1. If I am validating a form, should I set the form action to the controller method that originally displays the form, let's say I generate the form from a controller method called contact, or should I perform the validation in the contact method or perform it in the final method which could be something like feedback.

2. If I perform the validation in the feedback method, how can I redirect to the original view AND display the errors in the contact view.

Things I have seen
I found an interesting post from madmartigan1 that went as follows:-
Quote:LuckyFella73 - 19 December 2010 08:44 PM

Maybe you could set some if/else statemnts in your controller
and check which form was submitted. Set a hidden input field
for each form and check which form was send. Then in your
controller:
if ( isset($this->input->post('form_1')) and strlen($this->input->post('form_1'))>1)
{
// validate form 1
}elseif( isset($this->input->post('form_2')) and strlen($this->input->post('form_2'))>1)
{
// validate form 2
}elseif( isset($this->input->post('form_3')) and strlen($this->input->post('form_3'))>1)
{
// validate form 3
}

Didn’t test that but it should work.

This would work, but $this->input->post() will return false if it’s empty or not set, so all you would need is:
if ($this->input->post('form_1'))
{
// validate form 1
}
if ($this->input->post('form_2'))
{
// validate form 2
}

I use this sometimes when I have 2 or more forms that post to the same controller.
But I cannot seem to get that to work, because I am not sure how to implement madmartigan1's solution, I did ask in the thread but, it was quite old

Any advice would be greatly appreciated

Jon
#2

[eluser]Cristian Gilè[/eluser]
This a possible way to manage validation for more than one form in a view:

the controller:

Code:
<?php

class Test extends Controller
{
  
  function Test()
  {
    parent::Controller();
    $this->load->library('form_validation');
    $this->load->library('session');
    $this->load->helper('url');
  }
  
  function index()
  {
    $this->load->view('myforms');
  }
  
  function validation_one()
  {
    $this->form_validation->set_rules('name','name','required|min_length[5]');
    
    if($this->form_validation->run() !== FALSE)
    {
      $this->session->set_flashdata('success','You first form was submitted with success');
      redirect('test');
    }
    else
    {
      $this->index();
    }
    
  }
  
  function validation_two()
  {
    $this->form_validation->set_rules('lastname','lastname','required|min_length[3]');
    
    if($this->form_validation->run() != FALSE)
    {
      $this->session->set_flashdata('success','You second form was submitted with success');
      redirect('test');
    }
    else
    {
      $this->index();
    }
  }
  
}

the view (myforms.php):

Code:
<!DOCTYPE>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;my forms&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
echo $this->session->flashdata('success');
echo validation_errors();

//first form
echo form_open('test/validation_one');
echo form_label('name');
echo form_input('name',set_value('name'));
echo form_submit('send','send');
echo form_close();
//second form
echo form_open('test/validation_two');
echo form_label('lastname');
echo form_input('lastname',set_value('lastname'));
echo form_submit('send','send');
echo form_close();
?&gt;
&lt;/body&gt;
&lt;/html&gt;

I coded it quickly. I hope it works.

Cristian Gilè
#3

[eluser]edjon2000[/eluser]
Great Smile, Thanks again Cristian

I will try it out

Jon
#4

[eluser]edjon2000[/eluser]
Ok I have tried that solution and it will work if I have a separate controller for my contact page perhaps that might be a better way to design the whole site.

At the moment I have a Site controller and all the user pages of my site are different methods within the Site controller, would it be a better idea to give each page its own controller.

After spending some more time with it and rewriting the project with each page as a separate controller, either way seems to work, but, for some reason, it kills my flash banner, that, in itself is no big deal as I am using a freebie banner for it so that will probably not be staying on the live site I will probably replace it with my own flash version using the same image files or maybe replace it with some sort of javascript slideshow, not sure which is better for search engines.

Jon
#5

[eluser]edjon2000[/eluser]
Just had a couple of thoughts, has anyone found out if Flash Banners or Javascript Slideshow Banners are better for search engines.

And, is it better to construct a website in CI having each page as a separate controller or have one main controller and the pages as methods in terms of speed an example from my project would be a mainsite controller for the user pages and a separate admin controller for the admin pages, everywhere I look opinions seem to vary, so any advice about that would be greatly appreciated as well.

Jon




Theme © iAndrew 2016 - Forum software by © MyBB