Welcome Guest, Not a member yet? Register   Sign In
validation isn't working right
#1

[eluser]dadamssg[/eluser]
im trying to build a fairly complex validation script but i can't even get off the ground. my controller isn't even wanting to display the form. It displays 'main_view' when i try to go to the the right url when it should be displaying 'create_event'. 'create_event' is the view that holds my form

heres the relevant controller
Code:
function create()
    {
        if($this->session->userdata('logname')){}else
        {
        redirect('mains');
        }
    
    
        $data['notifications'] = $this->Loginmodel->get_notifications();
        $data['messages'] = $this->Loginmodel->get_messages();
        $data['title'] = "Publish Event";
        $this->load->view('header_view', $data);
        #####decide which views to load###################
        if($this->session->userdata('logname') == "")
        {
        $this->load->view('login_view', $data);
        }
        else
        {
        $this->load->view('logged_view', $data);
        }
        
        $this->load->library('validation');    
        $this->load->library('form_validation');
        $rules['title']    = "required|xss_clean";
        $rules['description']    = "required|xss_clean";
        $rules['location']    = "required|xss_clean";
        
        $this->validation->set_rules($rules);
        
        
                if ($this->validation->run() == FALSE)
                {
                $this->load->view('main_view', $data);    
                }
                else
                {
                    
                    $this->load->view('create_event', $data);
                }
        
        $this->load->view('footer_view');
    }
#2

[eluser]dadamssg[/eluser]
just realized that i had the main_view and create_event switched so i switched them but im not coming up with errors when i submit the form

heres my create_event view
Code:
<?php

echo validation_errors();

echo form_open('event/create');

$data = array(

              'name'        => 'username',

              'maxlength'   => '100',

              'size'        => '75',

            );



echo "Title: <br>".form_input($data)."<br><br>";



$descr = array(

              'name'        => 'description',

              'cols'   => '57',

              'rows'        => '6',

            );



echo "Description: <br>".form_textarea($descr)."<br><br>";



$location = array(

              'name'        => 'location',

              'maxlength'   => '100',

              'size'        => '75',

            );



echo "Location: <br>".form_input($location)."<br><br>";  

echo form_submit('submit', 'Publish');

echo form_close();

i have several dropdowns after location and before the submit button but the character limit on here wouldn't let me post the whole thing
#3

[eluser]danmontgomery[/eluser]
Any reason to be using the old validation library instead of form_validation?

I haven't set rules the way you are, but I believe they are being set incorrectly:

http://ellislab.com/codeigniter/user-gui...lesasarray

Quote:$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'Password Confirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);

$this->form_validation->set_rules($config);
#4

[eluser]dadamssg[/eluser]
i got it working. But i have a new question. Where do you put a custom error message? i have the following but its still outputting the old error message.

Code:
if ($this->validation->run() == TRUE)
                {
                $this->load->view('main_view');    
                }
                else
                {
                    $this->form_validation->set_message('required', 'The % is required');
                    $this->load->view('create_event');
                }
#5

[eluser]Dyllon[/eluser]
You seem to be mixing two different libraries, make sure you aren't getting validation and form_validation confused.
Validation is deprecated.
#6

[eluser]dadamssg[/eluser]
its not working...i have this below...it gets the other others for the required fields but not for my callback
Code:
$this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Title', 'trim|required');
        $this->form_validation->set_rules('location', 'Location', 'trim|required');
        $this->form_validation->set_rules('description', 'Description', 'trim|required');
        $this->form_validation->set_rules('smonth', 'Start Month', 'callback_start_date');
        
        
        $this->form_validation->set_error_delimiters('<h5>', '</h5>');

                if ($this->form_validation->run() == TRUE)
                {
                $this->load->view('main_view');    
                }
                else
                {
                    $this->form_validation->set_message('start_date', 'Your start date doesnt exists.');
                    $this->load->view('create_event');
                }
        
        $this->load->view('footer_view');
    }
    
    
    function end_date()
    {
        if(checkdate($_POST['emonth'], $_POST['eday'], $_POST['eyear']))
        {
        return TRUE;
        }else
        {
        return FALSE;
        }
    }
    
    function start_date()
    {
        if(checkdate($_POST['smonth'], $_POST['sday'], $_POST['syear']))
        {
        return TRUE;
        }else
        {
        return FALSE;
        }
    }
#7

[eluser]Dyllon[/eluser]
Have you attempted doing an echo to confirm it hits your callback function?
#8

[eluser]dadamssg[/eluser]
i know its hitting the callback because it redirects back to the form when i input say feb 31 2010 and if enter a valid date it redirects as if it were correct
#9

[eluser]dadamssg[/eluser]
i get this

"Unable to access an error message corresponding to your field name."
#10

[eluser]dadamssg[/eluser]
ah i figured it out...i had to put it into the start_date function. new questoin....I have a variables that contain the numbers of the current month, day, and year($mo, $da, and $yr). How do i use those to select the current day in this select field. I know i have to use set_select() but you have to use "TRUE" instead of actual values...
heres what i have
Code:
$sday = array(

                  '1'  => '1',

                  '2'  => '2',

                  '3'  => '3',

                  '4'  => '4',

                  '5'  => '5',

                  '6'  => '6',

                  '7'  => '7',

                  '8'  => '8',

                  '9'  => '9',

                  '10'  => '10',

                  '11'  => '11',

                  '12'  => '12',

                  '13'  => '13',

                  '14'  => '14',

                  '15'  => '15',

                  '16'  => '16',

                  '17'  => '17',

                  '18'  => '18',

                  '19'  => '19',

                  '20'  => '20',

                  '21'  => '21',

                  '22'  => '22',

                  '23'  => '23',

                  '23'  => '23',

                  '24'  => '24',

                  '25'  => '25',

                  '26'  => '26',

                  '27'  => '27',

                  '28'  => '28',

                  '29'  => '29',

                  '30'  => '30',

                  '31'  => '31',

                );



echo form_dropdown('sday', $sday, set_value('sday', $da))." ";




Theme © iAndrew 2016 - Forum software by © MyBB