Multiple Validations ..

#1
[eluser]elmystica[/eluser]
I have a contactpage.
It has a menu, headerfield, left side is a contactform, right side there is a small login form ( on every page of the site ).

As you can see, I have a validateMail function to check on the contact-form after submit, and a validateLogin function after submit on the contact-form.

The problem is that both get called if you submit one of them, and so they both get error-messages.

How to fix that?

(if I try removing the validation calls in the index function, I get errors all along ...)

Code:
<?php

class Contact extends Controller{
    
    function Contact(){
        
        parent::Controller();
        $this->load->model('navigation_utils');
        $this->load->model('page_content');
        $this->load->helper('form');
        $this->load->library('validation');
        
    }
    
    function index(){
        
        $data['menu'] = $this->navigation_utils->displayMenu();
        
        $page = $this->uri->segment(1);
        if($page == '')
        {
            $page = "home";
        }
        $data['basic_page_title'] = $this->page_content->getBasicTitle( $this->navigation_utils->getPageId($page) );
        
        if($_POST['submit'] = "Verzenden"){
            $this->validateMail();
        }
        if($_POST['submit'] = "Aanmelden"){
            $this->validateLogin();
        }
        $this->load->view('head');
        $this->load->view('header', $data);
        $this->load->view('contactform', $data);
        $this->load->view('login_form');
        $this->load->view('footer');
    }
    
    function login()
    {
        $this->validateLogin();
                
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        if ($this->validation->run() == FALSE)
        {
            $this->index();
        }
        else
        {
            redirect('cms');
        }
        
    }
    
    function validateLogin()
    {
        $rules['mail']    = "trim|required|valid_email";
        $rules['pass']    = "trim|required|min_length[5]|max_length[12]";
        
        $this->validation->set_rules($rules);
        
        $fields['mail'] = "Emailadres";
        $fields['pass'] = "wachtwoord";
        
        $this->validation->set_fields($fields);
    }
    
    function validateMail()
    {
        $rules['voornaam']        = "trim|required";
        $rules['familienaam']    = "trim|required";
        $rules['mail']            = "trim|required|valid_mail";
        $rules['mailconfirm']    = "trim|required|valid_mail";
        $rules['onderwerp']        = "trim|required";
        $rules['bericht']        = "trim|required";
        
        $this->validation->set_rules($rules);
        
        $fields['voornaam']        = "Voornaam";
        $fields['familienaam']    = "Familienaam";
        $fields['mail']            = "Emailadres";
        $fields['mailconfirm']    = "Emailadres bevestigen";
        $fields['onderwerp']    = "Onderwerp";
        $fields['bericht']        = "Uw bericht";
        
        $this->validation->set_fields($fields);
    }
    
    function mail()
    {
        $this->validateMail();
        $this->validation->set_error_delimiters('<span class="error"> ', '</span>');
            
        if( $this->validation->run() == FALSE )
        {
            $this->index();
        }
        else
        {
            echo $_POST['voornaam'];
        }
    }
    
}

?&gt;

#2
[eluser]Unknown[/eluser]
In your index function you made a small mistake

Code:
if($_POST['submit'] = "Verzenden"){
            $this->validateMail();
        }
        if($_POST['submit'] = "Aanmelden"){
            $this->validateLogin();
        }

Should be ...

Code:
if($_POST['submit'] == "Verzenden"){
            $this->validateMail();
        }
        if($_POST['submit'] == "Aanmelden"){
            $this->validateLogin();
        }

You're accidentally setting the $_POST variables instead of comparing them.

Simple mistake -- I make the same one more often than I'd like to admit Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.