Welcome Guest, Not a member yet? Register   Sign In
Validation error on first view
#1

[eluser]Mutsop[/eluser]
Hi,

For some reason I get Undefined property errors of my validations...
controller:
Code:
<?php
class Contact extends Controller {

    function Contact(){
        parent::Controller();

            // load helper
        $this->load->helper(array('form','url'));
        $this->load->library('validation');

    }
    function Index(){
        $data['attributes'] = array('class' => 'submit', 'type' => 'submit', 'name' => 'submit', 'value' => 'Verzenden');
        $data['heading'] = 'Contact';
        $this->load->view('header');
        $this->load->view('navigation');
        $this->load->view('contact', $data);
        $this->load->view('footer');
    }
    function submit() {
        $this->_set_fields();
        $this->_set_rules();
        
        if ($this->validation->run() == FALSE){
            $data['message'] = '';
        }else{
            $data['message'] = 'Bericht succesvol gestuurd';
        }
        $data['heading'] = 'Contact';
        $data['attributes'] = array('class' => 'submit', 'type' => 'submit', 'name' => 'submit', 'value' => 'Verzenden');
        
        $this->load->view('header');
        $this->load->view('navigation');
        $this->load->view('contact', $data);
        $this->load->view('footer');
    }
    
    function _set_fields(){
        $fields['voornaam'] = 'voornaam';
        $fields['email'] = 'email';
        $fields['commentaar'] = 'commentaar';

        $this->validation->set_fields($fields);
    }

    // validation rules
    function _set_rules(){
        $rules['voornaam'] = 'trim|required';
        $rules['email'] = 'trim|required|valid_email';
        $rules['commentaar'] = 'trim|required';

        $this->validation->set_rules($rules);

        $this->validation->set_message('required', '* verplicht');
        $this->validation->set_message('isset', '* verplicht');
        $this->validation->set_error_delimiters('<p class="error">', '</p>');
    }
}?&gt;

View:
Code:
<section id="content">
    <div id="wrapper">
        <div class="bg1 pad">
            <h3>&lt;?php echo $heading; ?&gt;</h3>
            <article class="col2">
                &lt;?php echo form_open('contact/submit'); ?&gt;
                <p>
                    <label for="voornaam">Voornaam / naam: </label>
                    &lt;input type="text" name="voornaam" class="text" value="&lt;?php echo $this-&gt;validation->voornaam; ?&gt;"/>
                    &lt;?php echo $this->validation->voornaam_error; ?&gt;
                </p>
                <p>
                    <label for="email">E-mail: </label>
                    &lt;input type="text" name="email" class="text" value="&lt;?php echo $this-&gt;validation->email; ?&gt;"/>
                    &lt;?php echo $this->validation->email_error; ?&gt;
                </p>
                <p>
                    <label for="commentaar">Commentaar: </label>
                    &lt;textarea name="commentaar"&gt;&lt;?php echo $this->validation->commentaar; ?&gt;&lt;/textarea&gt;
                    &lt;?php echo $this->validation->commentaar_error; ?&gt;
                </p>
                <p>
                    &lt;?php echo form_submit($attributes); ?&gt;
                </p>
                &lt;?php echo form_close(); ?&gt;
            </article>
            <article class="col1">
            
            </article>
        </div>
    </div>
</section>

So on each of my &lt;?php echo $this->validation->xxxxx; ?&gt; and _error I get the Undefined property error... As you can see, I do declare them trhough the rules and fields.

So why?
#2

[eluser]cideveloper[/eluser]
Do you get the errors the first time you load the page and not when you submit?

Your index function does not have validation fields set. I dont know why everyone seems to be breaking the form validation into multiple functions. Is there a specific reason for this?

Look at this post where I show form validation working perfectly.
#3

[eluser]Mutsop[/eluser]
Well I've been told many times to program like that.
To be honest, there are so many ways you can program I just don't know how to anymore Smile

But I guess for such small script it isn't necessary.

And yes it is on the first visit I get this code. And now I do see where my problem lies...

Thanks a bunch




Theme © iAndrew 2016 - Forum software by © MyBB