Welcome Guest, Not a member yet? Register   Sign In
Multiple form validation problem
#1

[eluser]ppwalks[/eluser]
I have 2 forms on an include in a sidebar and I want to validate them both seperatley but the problem I have is if I click the button on one form it validates them both, the issue of them working within a sidebar I have figured out but I can get them to validate individualy.

Heres the code:

Code:
function validateQuickQuote() {
        
         $this->form_validation->set_rules('name', 'name', 'required|min_length[3]');
         $this->form_validation->set_rules('email', 'email', 'required|valid_email');
         $this->form_validation->set_rules('contact', 'Telephone', 'required|numeric|min_length[9]|is_uk_num');
         $this->form_validation->set_rules('date', 'Expected Date', 'required');
         $this->form_validation->set_rules('moving_frm', 'Moving From', 'required');
         $this->form_validation->set_rules('moving_to', 'Moving To', 'required');
        
        
        }
        
        function validateSubscribe() {
  
        
         $this->form_validation->set_rules('sub_name', 'name', 'required|min_length[3]');
         $this->form_validation->set_rules('sub_email', 'email', 'required|valid_email');
        
        }

Above is my model code

Now my controller

Code:
function index()
{
  $data['action'] = "index";
  $data['title'] = "";
  $data['main'] = 'home_main';
  $data['side'] = 'sidebar';    
  $data['testim'] = $this->MRemovals->getRandomTestimonials(3);
  $data['latestNews'] = $this->MPosts->getFrontPagePosts();
  $data['latestPromos'] = $this->MPosts->getFrontPagePromos();
  $this->MRemovals->validateSubscribe();
  $this->MRemovals->validateQuickQuote();
  if ($this->form_validation->run() == FALSE)
  {
  $this->load->vars($data);
  $this->load->view('index');
  }

I have tried an

Code:
if ($this->input->post('quick-quote')) {

wrapped around each function and even tried an if / else within the contrioller but still nothing.

Can anyone assist with this as it is driving me insane...

Thank you in advance

Paul
#2

[eluser]ppwalks[/eluser]
If anyone knows please post back as of now my only solution is to use another library such as pear or zend to finish the job but i'm really wanting to use cl
#3

[eluser]CroNiX[/eluser]
Submit each form to it's own method. Each method would have it's own rules (like you have) AND each one will run it's own $this->form_validation->run(). Right now in your index method, you are setting the rules for both forms AND running the form validation on both.

Also, in your view, the forms cannot overlap (HTML standards). Each one should have it's own <form></form> with a submit within each. You can name your submit different for each one and do something like:
Code:
//check if form 1 was submitted, if so, load rules for that form and set flag to TRUE
$form1_submitted = $this->input->post('name-of-your-submit-for-form1');
$form_submitted = FALSE;
if ($form1_submitted)
{
  $this->validateQuickQuote();
  $form_submitted = TRUE;
}

//check if form 2 was submitted, if so, load rules for that form and set flag to TRUE
$form2_submitted = $this->input->post('name-of-your-submit-for-form2');
if ($form2_submitted)
{
  $this->validateSubscribe();
  $form_submitted = TRUE;
}

//check if one of the forms was submitted, if so, run the validation on that form
if ($form_submitted)
{
  if ($this->form_validation->run() === FALSE)
  {
    //display form with errors
  }
  else
  {
    //success
  }
}
#4

[eluser]ppwalks[/eluser]
Hi thanks for the responce, the code you gave me doesn't work either, it blanks out the page when I put it in, also my forms are separate with separate inputs I will post my sidebar view code here..

I love codeigniter but surely they must anticipate that users would want easier form submission than this, at this rate I'll be using another library such as zend form to complete the task unless another remedy is available.

view code:

Code:
<div id="top"></div>
<div id="middle">
&lt;?php
$attributes = array('class' => 'quick-quote', 'id' => 'quick-quote', 'name' => 'quick-quote' );
echo form_open("welcome/$action", $attributes) . "\n";
echo form_error('name');
echo "<label for='name'>Name</label><br/>";
$data = array(
              'name'        => 'name',
              'id'          => 'name',
              'maxlength'   => '100',
     'class'   => 'input',
            );

echo "<div>" .form_input($data)."</div> \n";
echo form_error('email');
echo "<label for='email'>Email</label><br/>";
$data = array(
              'name'        => 'email',
              'id'          => 'email',
              'maxlength'   => '100',
     'class'   => 'input',
            );

echo "<div>" .form_input($data)."</div> \n";
echo form_error('contact')  . "<br />";
echo "<label for='contact'>Contact Number</label><br/>";
$data = array(
              'name'        => 'contact',
              'id'          => 'contact',
              'maxlength'   => '100',
     'class'   => 'input',
            );

echo "<div>" .form_input($data)."</div> \n";
echo form_error('date')  . "<br />";
echo "<label for='date'>Expected Date</label><br/>";
$data = array(
              'name'        => 'date',
              'id'          => 'datepicker',
              'maxlength'   => '100',
     'class'   => 'datepicker input',
            );

echo "<div>" .form_input($data)."</div> \n";
echo form_error('moving_frm')  . "<br />";
echo "<label for='moving from'>Moving From</label><br/>";
$data = array(
              'name'        => 'moving_frm',
              'id'          => 'moving_frm',
              'maxlength'   => '100',
     'class'   => 'input',
            );

echo "<div>" .form_input($data)."</div> \n";
echo form_error('moving_to')  . "<br />";
echo "<label for='moving to'>Moving To</label><br/>";
$data = array(
              'name'        => 'moving_to',
              'id'          => 'moving_to',
              'maxlength'   => '100',
     'class'   => 'input',
            );

echo "<div>" .form_input($data)."</div> \n";

$reset_btn = array(
                    'type'      => 'reset',
                    'name'        => 'image',
     'id'        => 'reset-btn',
                    'value'        => ''
                    
                );
$submit_btn = array(
                    'type'      => 'image',
                    'src'        => base_url().'images/form-submit-btn.png',
                    'name' => 'quick-quote',
     'id'        => 'submit-btn',
                    'width'     => '75',
                    'height'    => '39',
                    'value'        => ''
                    
                );

echo form_input($reset_btn);
echo form_input($submit_btn);
echo form_close();
?&gt;
    </div>
    <div id="bottom"></div>
     <div id="testimonials">
        <br /><br />
        &lt;?php
    foreach ($testim as $key => $list){
    echo "<strong>" .$list['name']."</strong> \n";
    echo "<p> ". auto_typography(word_limiter($list['ShortDesc'], 19)) ."<p> \n";
    echo img('images/side-bar-divide.png');
    
    
    }
    ?&gt;
        </div>
        <div id="subscribe-to-news">
        &lt;?php
$attributes = array('class' => 'newsletter', 'id' => 'newsletter', 'name' => 'register');
echo form_open("welcome/$action", $attributes) . "\n";
echo "<label for='name'>Name</label><br/>";
echo form_error('sub_name');
$data = array(
              'name'        => 'sub_name',
              'id'          => 'name',
              'maxlength'   => '100',
     'class'   => 'input',
            );

echo "<div>".form_input($data)."</div> \n";
echo "<label for='email'>Email</label><br/>";
echo form_error('sub_email');
$data = array(
              'name'        => 'sub_email',
              'id'          => 'email',
              'maxlength'   => '100',
       'class'   => 'input',
            );
$btn_register = array(
                    'type'      => 'image',
                    'src'        => base_url().'images/submit-btn.png',
                    'name'        => 'register',
     'id'        => 'newsletter-btn',
                    'width'     => '109',
                    'height'    => '73',
                    'value'        => ''
                    
                );

echo "<div>" .form_input($data)."</div>";
echo form_input($btn_register);
echo form_close();
?&gt;
        </div>

Thanks again big help...
#5

[eluser]mast3rpyr0[/eluser]
This is how I do my forms. A user logs in with my form, and it is submitted to the account controller, using the login function. (www.site.com/account/login). Login does the validation and then whatever else i need to use the input for is done. If i were to create a registration function, I would redfine it exactly the same way, making modifications for that specific form. My rules are defined in the controller, after validation is passed I send my valid inputs to my models to check databases and such.

Hope this helps

EDIT: Just saw the code you used for your forms.. good lord just use html, the form helper class is ridiculous lol.

Controller:
Code:
public function index()  // www.site.com/account
{  
  $this->load->view('login_form');
}

public function login()  // www.site.com/account/login
{
  $this->load->library('form_validation');
  $this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[24]');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');

  if($this->form_validation->run() == false)
  {
   $this->load->view('login_form');
  }
  else
  {
   $username = $this->input->post('username');
   $password = $this->input->post('password');

   $this->load->model('account_model');

   //use model functions to log the user in.
  }
}
public function register()  // www.site.com/account/register
{
  $this->load->library('form_validation');
  $this->form_validation->set_rules('reguser', 'Username', 'trim|required|max_length[24]|is_unique[users.username]');
  $this->form_validation->set_rules('regpass', 'Password', 'trim|required|md5');
  $this->form_validation->set_rules('regemail', 'Email', 'trim|required|valid_email|is_unique[users.email]');

  if($this->form_validation->run() == false)
  {
   $this->load->view('register_page');
  }
  else
  {
   $username = $this->input->post('reguser');
   $password = $this->input->post('regpass');
   $email = $this->input->post('regemail');
  
   $this->load->model('account_model');

   //use model functions to register the user in db.
  }
}
form:
Code:
&lt;form method="post" action="&lt;?=base_url()?&gt;account/login"&gt;
      &lt;input type="text" name="username" id="username" placeholder="Username" /&gt;
      &lt;input type="password" name="password" id="password" placeholder="Password" /&gt;
      &lt;input type="submit" value="Submit" /&gt;
&lt;/form&gt;

&lt;form method="post" action="&lt;?=base_url()?&gt;account/register"&gt;
       &lt;input type="text" name="reguser" id="reguser" placeholder="Username" /&gt;
       &lt;input type="password" name="regpass" id="regpass" placeholder="Password" /&gt;
       &lt;input type="email" name="regemail" id="regemail" placeholder="Email" /&gt;
       &lt;input type="submit" value="Submit" /&gt;
&lt;/form&gt;
#6

[eluser]mast3rpyr0[/eluser]
double post
#7

[eluser]ppwalks[/eluser]
Thanks for the reply but you didn't answer the question I was asking which was regarding 2 forms on one page that can validate without effecting the other form, I have tried to use conditional statements within the controller but it always validates both forms from a single click of either submit button.

I use the form helper as I find it easier to read when looking for form attributes, theres always a reason to a persons madness!
#8

[eluser]mast3rpyr0[/eluser]
Lol whatever works for you I guess. I dont see what you mean? With my code above when the login form is submitted, only the login rules are applied. When register is submitted, those rules are used. Make sure both of your forms are in separate &lt;form&gt;&lt;/form> blocks, and that you dont define rules for fields other than the ones you are accepting for the one specific form.
#9

[eluser]InsiteFX[/eluser]
I do not see what the problem is, CroNiX showed you above how to check your forms!
Maybe have some bad coding logic someplace else in your code...

Also I do not see your if($this->form_validation->run() == false)
#10

[eluser]ppwalks[/eluser]
Its there, the problem is as a full statement I'm a bit confused where to load the initial view as when it returns the process it calls the view again which may be causing it to hault...




Theme © iAndrew 2016 - Forum software by © MyBB