Welcome Guest, Not a member yet? Register   Sign In
Form validation shows only last error! pls help!
#1

[eluser]esthezia[/eluser]
hello!

i searched for hours on the internet and didn't find a solution for my problem! pls help!

i'm a beginner with codeigniter. my problem is with form validation library. i have a simple form with a few inputs and i want to validate them: some of them are required, some are not. if i don't write anything in any field there should be an error for every empty field that is required, but instead there is only the error for the last validated input!

here's my view:

Code:
<?php echo form_open(); ?>
            <div class="section">website title</div>
            <ul class="data">
                <li class="req"><label>Website title</label>&lt;input type="text" name="website" value="&lt;?php echo set_value("website", ""); ?&gt;" /&gt;&lt;/li>
            </ul>
            <div class="clear"></div>
            <div class="section">admin user</div>
            <ul class="data">
                <li class="req"><label>Username</label>&lt;input type="text" name="username" value="&lt;?php echo set_value("username", ""); ?&gt;" /&gt;&lt;/li>
                <li class="req"><label>Password</label>&lt;input type="password" name="password" value="&lt;?php echo set_value("password", ""); ?&gt;" /&gt;&lt;/li>
                <li class="req"><label>Email address</label>&lt;input type="text" name="email" value="&lt;?php echo set_value("email", ""); ?&gt;" /&gt;&lt;/li>
                <li><label>First name</label>&lt;input type="text" name="first_name" value="&lt;?php echo set_value("first_name", ""); ?&gt;" /&gt;&lt;/li>
                <li><label>Last name</label>&lt;input type="text" name="last_name" value="&lt;?php echo set_value("last_name", ""); ?&gt;" /&gt;&lt;/li>
            </ul>
            <div class="clear"></div>
            &lt;?php echo validation_errors("<p class=\"error\">", "</p>"); ?&gt;
            &lt;input type="submit" value="INSTALL" class="submit_btn" /&gt;
        &lt;?php echo form_close(); ?&gt;

here's the function in my controller that handles the form:


Code:
function index () {
        
        if (!$this->input->post()) {
            $this->load->view("install");
            return;
        }
        
        $this->form_validation->set_rules("website", "website", "required|trim|xss_clean");
        $this->form_validation->set_rules("username", "username", "required|trim|alpha_numeric|xss_clean");
        $this->form_validation->set_rules("password", "password", "min_length[5]|trim|xss_clean");
        $this->form_validation->set_rules("email", "email", "required|trim|valid_email|xss_clean");
        $this->form_validation->set_rules("first_name", "first name", "trim|xss_clean");
        $this->form_validation->set_rules("last_name", "last name", "trim|xss_clean");
        
        if ($this->form_validation->run() === FALSE) {
            $this->load->view("install");
            return;
        }
        
        $this->load->view("install");
        
    }

if i hit submit, there is no error showing because the last validation rule doesn't contain "required" and thus it is not visible, but if i let only the required rules, it shows only "the email field is required" and that's it.

pls if someone can help me it would be much appreciated!

thank you!
#2

[eluser]Irfan Cikoglu[/eluser]
You havent any validation rule on first_name and last_name, so this is problem. Use this controller Wink
Code:
public function index () {
        if (!$this->input->post()) {
            $this->load->view("install");
        }        
        $this->form_validation->set_rules("website", "website", "trim|required|xss_clean");
        $this->form_validation->set_rules("username", "username", "trim|required|alpha_numeric|xss_clean");
        $this->form_validation->set_rules("password", "password", "trim|min_length[5]|xss_clean");
        $this->form_validation->set_rules("email", "email", "trim|required|valid_email|xss_clean");
        $this->form_validation->set_rules("first_name", "first name", "trim|min_lenght[3]|xss_clean");
        $this->form_validation->set_rules("last_name", "last name", "trim|min_lenght[3]|xss_clean");
        
        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view("install");
        }
        else
        {
            $this->load->view("install");
        }
    }
If you still have problem, then remove first_name and last_name rules and manually trim and clean XSS Wink
#3

[eluser]esthezia[/eluser]
hi! thanks for your reply!

i also tried commenting the "last_name" and "first_name" rules, and leave all the others and didn't work; and also tried with only the first 2 rules set (website and username) which are both required, and didn't work as well (it was outputting only the error for username). so.. your idea doesn't work unfortunately.

i want to mention that i have set the form_validation class to autoload, in config file.

thank you!
#4

[eluser]esthezia[/eluser]
i solved it! the problem was because i changed the Form_validation library by inserting a line as mentioned here. i thought that it was a fix that couldn't hurt me.. but it did.

Smile




Theme © iAndrew 2016 - Forum software by © MyBB