Welcome Guest, Not a member yet? Register   Sign In
validation questions
#1

[eluser]jorre[/eluser]
I'm looking for a solution for the following (CI-newb) situation:

I'm validating a form on VIEW1 -> works perfect
-> when validation is successful I do the following:

Code:
$this->load->view('header');        
                            $this->load->view('pollcreate_step2', $data);    //reload step 2
                            $this->load->view('footer');

Ok so all goes well, my second view is shown, and I have stored the form data from VIEW1 in a session variable, so I can check if that form was already processed

Now it goes wrong when validating this second VIEW. I do

Code:
if($this->validation->run() == FALSE)

to validate my second view, with some new rules, but that never succeeds, since there are still errors showing up saying that my first VIEW form is not filled in (because this is not shown anymore)...

Does anyone know if I can tell CI to just check for the $rules on my second form, and not my first form anymore?

Here is my full controller code


Code:
<?php
      class Poll extends Controller
    {
                                  
        function Poll()
        {
            parent::Controller();
        }
        
        function index()
        {
            $this->load->view('pollview');            
        }
        
        function create()
        {
            
            $this->load->library('session');
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            
            
            //set validation rules
            $rules['poll_topic'] = "trim|required";
            $rules['poll_yourname'] = "trim|required";
            $rules['poll_youremail'] = "trim|valid_email";
            $this->validation->set_rules($rules);
            
            //set default names for display in error message
            $fields['poll_topic'] = 'Poll topic';
            $fields['poll_yourname'] = 'Your name';
            $fields['poll_youremail'] = 'Your email address';
            $this->validation->set_fields($fields);
            
            //apply custom error formatting
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            $this->validation->set_message('required', '%s is required');
                        
            //run form validation check            
            if($this->validation->run() == FALSE && $this->session->userdata('poll_topic') == '')
            {
                //SHOW STEP 1        
                $this->load->view('header');    
                $this->load->view('pollcreate_step1');    
                $this->load->view('footer');    
            }
            else
            {    
                //VALIDATION OF STEP1 IS OK !            
                //STORE DATA FROM STEP 1 IN A SESSION
                if ($this->session->userdata('poll_topic') == '')
                {
                    //SESSION DOES NOT YET EXIST, SO LET'S FILL IT
                    $data = array(
                       'poll_topic' => $this->input->post('poll_topic'),
                       'poll_yourname' => $this->input->post('poll_yourname'),
                       'poll_youremail' => $this->input->post('poll_youremail')
                    );
                    $this->session->set_userdata($data);
                }
                else
                {
                    //SESSION DATA EXISTS, LET'S FILL OUR $DATA VAR TO WORK WITH IN VIEW STEP2
                    $data = array(
                       'poll_topic' => $session_id = $this->session->userdata('poll_topic'),
                       'poll_yourname' => $session_id = $this->session->userdata('poll_yourname'),
                       'poll_youremail' => $session_id = $this->session->userdata('poll_youremail')
                    );                    
                }
                
                        //SHOW STEP 2                            
                        $rules['poll_choice1'] = "trim|required";
                        $rules['poll_choice2'] = "trim";    
                        $this->validation->set_rules($rules);
                        
                        //set default names for display in error message
                        $fields['poll_choice1'] = 'At least one option must be available in any poll';
                        $this->validation->set_fields($fields);
                                    
                        //apply custom error formatting
                        $this->validation->set_error_delimiters('<div class="error">', '</div>');
                        $this->validation->set_message('required', '%s');
                        
                        //run form validation check            
                        if($this->validation->run() == FALSE)
                        {
                            //SHOW STEP 2
                            $this->load->view('header');        
                            $this->load->view('pollcreate_step2', $data);    //reload step 2
                            $this->load->view('footer');    
                        }
                        else
                        {
                            //LOAD THE DATABASE MODEL AND INSERT THE NEW POLL
                            $this->load->model('Poll_model','', TRUE);
                            $this->Poll_model->add_poll_options();
                            
                            $this->load->view('header');    
                            $this->load->view('messages/poll_success');    //show success message
                            $this->load->view('footer');    
                        }
                            
                
            }
        }
        
        
        
                
        
    }
?&gt;
#2

[eluser]xwero[/eluser]
The code seems a bit strange. You don't check if there is post data so when you go to http://site.com/poll/create you are validating for the first time already?

My take on your code is as follows
Code:
function create()
{
    $this->load->view('header');

    $this->load->library('session');
    $this->load->helper(array('form', 'url'));
    $this->load->library('validation');
            
    
    if(isset($_POST['step1'])
    {
       //set validation rules
       $rules['poll_topic'] = "trim|required";
       $rules['poll_yourname'] = "trim|required";
       $rules['poll_youremail'] = "trim|valid_email";
            
       //set default names for display in error message
       $fields['poll_topic'] = 'Poll topic';
       $fields['poll_yourname'] = 'Your name';
       $fields['poll_youremail'] = 'Your email address';
     }

     if(isset($_POST['step2']))
     {
        $rules['poll_choice1'] = "trim|required";
         $rules['poll_choice2'] = "trim";    
                        
         //set default names for display in error message
         $fields['poll_choice1'] = 'At least one option must be available in any poll';
      }
    if(count($_POST) > 0)
    {
       $this->validation->set_rules($rules);
       $this->validation->set_fields($fields);
    }
    //apply custom error formatting
    $this->validation->set_error_delimiters('<div class="error">', '</div>');
    $this->validation->set_message('required', '%s is required'); // where is this for???
    
    if($this->session->userdata('poll_topic') !== false)
    {
       $data = array(
                 'poll_topic' => $session_id = $this->session->userdata('poll_topic'), // why the $session_id = ???
                 'poll_yourname' => $this->session->userdata('poll_yourname'),
                 'poll_youremail' => $this->session->userdata('poll_youremail')
               );
    }
    //run form validation check  
    if(count($_POST) > 0)
    {          
    if($this->validation->run() == FALSE)
    {
        if(isset($_POST['step1'])
        {
            $this->load->view('pollcreate_step1');    
         }
         if(isset($_POST['step2'])
        {
            $this->load->view('pollcreate_step2', $data);
        }  
    }
    else
    {    
        if(isset($_POST['step1']))
        {
            //SESSION DOES NOT YET EXIST, SO LET'S FILL IT
            $data = array(
                 'poll_topic' => $this->input->post('poll_topic'),
                 'poll_yourname' => $this->input->post('poll_yourname'),
                 'poll_youremail' => $this->input->post('poll_youremail')
               );
             $this->session->set_userdata($data);
         }
         if(isset($_POST['step2']))
         {    
            //LOAD THE DATABASE MODEL AND INSERT THE NEW POLL
               $this->load->model('Poll_model','', TRUE);
               $this->Poll_model->add_poll_options();
                            
               $this->load->view('header');    
               $this->load->view('messages/poll_success');    //show success message
               $this->load->view('footer');    
         }
     }
     }
     // Show not posted forms
     if(count($_POST) == 0 && $this->session->userdata('poll_topic') !== false)
     {
        $this->load->view('pollcreate_step1');
     }
     if(count($_POST) == 0 && is_string($this->session->userdata('poll_topic')))
     {
        $this->load->view('pollcreate_step2',data);
     }
     $this->load->view('footer');
}
it's not tested but it looks better to me. Let me know if you try it.
#3

[eluser]jorre[/eluser]
Thanks a lot for correcting my newb-logic Smile

Can you tell me what the following does:

if(count($_POST) > 0) -> does this check if any post happened?
if(isset($_POST['step1'])) -> what is 'step1' in this code?


Code is not working, but when you elaborate the above two rules, I'm sure I can figure it out. Thanks for not giving up an a OOP-newbie! Wink
#4

[eluser]xwero[/eluser]
Quote:if(count($_POST) > 0) -> does this check if any post happened?
Yes
Quote:if(isset($_POST[’step1’])) -> what is ‘step1’ in this code?
It is the name of the submit. If you name the submit inputs you can distinguish actions on the same form but in this case it identifies the form.
#5

[eluser]jorre[/eluser]
So it's &lt;form name="step1"&gt; that identifies the if(isset($_POST[’step1’])) ?
#6

[eluser]xwero[/eluser]
no
&lt;input type="submit" name="step1" value="Proceed"&gt;
#7

[eluser]jorre[/eluser]
Perfect, that makes good sense! Thanks a lot for this tip.

I'm getting close, but right now I'm getting the following errors:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Validation::$poll_topic_error
Filename: views/pollcreate_step1.php
Line Number: 3

I guess this is due to the fact that fields & rules are not setup unless the form has been posted.
In my view I use code like

&lt;?=$this->validation->poll_topic_error; ?&gt;<br />

just as described in the userguide


Is there a way to avoid this error ?
#8

[eluser]xwero[/eluser]
You can set default validation values in your controller
Code:
$this->validation->poll_topic_error = '';
$this->validation->poll_topic = '';




Theme © iAndrew 2016 - Forum software by © MyBB