Welcome Guest, Not a member yet? Register   Sign In
Trouble with form validation
#1

[eluser]RickRaven[/eluser]
Hi, i am new to CodeIgniter, and currently i am working with the form validation class. However it seems it doesnt work for me.

Here is the code i am working with :

Controller
Code:
public function register()
    {
        $this->load->library("form_validation");

        $this->form_validation->set_rules("firstname","Firstname","required");
        $this->form_validation->set_rules("lastname","Lastname","required");
        $this->form_validation->set_rules("username","Username","required");
        $this->form_validation->set_rules("email","E-Mail","required");
        $this->form_validation->set_rules("email-validate","Re-Enter E-Mail","required");
            
        $isPostBack = $this->input->post("isPostBack");
        if($isPostBack=="true")
        {
            if($this->form_validation->run()==TRUE)
            {
                //database operations will be here
                
                redirect("page/display/userRegistrationSuccessful");
            }
        }
        
        $this->load->helper("url");
        $this->load->helper("form");
        $this->load->helper("style");
        
        
        $page["menu"] = $this->load->view("menu","", true);
        $page["top"] = $this->load->view("search_area", "", true);
        $page["content"] = $this->load->view("user/registration","", true);
        
        $page["title"] = "Registration";
        $page["theme"] = "default";
        $page["styles"] = array("main", "menu", "search_area", "user/registeration");
        $this->load->view("layouts/default", $page);
    }

View
Code:
<div id="registration">
&lt;form method="post"&gt;
<fieldset>
<legend>User Registration<legend>
<ul>
    <li>&lt;?=form_error("firstname")?&gt;</li>
    <li><span id="label">Firstname : </span>&lt;input type="text" id="firstname" maxlength="20" value="&lt;?=set_value("firstname")?&gt;" /&gt;&lt;/li>
    <li>&lt;?=form_error("lastname")?&gt;</li>
    <li><span id="label">Lastname : </span>&lt;input type="text" id="lastname" maxlength="20" value="&lt;?=set_value("lastname")?&gt;" /&gt;&lt;/li>
    <li>&lt;?=form_error("username")?&gt;</li>
    <li><span id="label">Username : </span>&lt;input type="text" id="username" maxlength="20" value="&lt;?=set_value("username")?&gt;" /&gt;&lt;/li>
    <li>&lt;?=form_error("email")?&gt;</li>
    <li><span id="label">E-Mail : </span>&lt;input type="text" id="email" maxlength="60" value="&lt;?=set_value("email")?&gt;" /&gt;&lt;/li>
    <li>&lt;?=form_error("email-validate")?&gt;</li>
    <li><span id="label">Re-enter E-mail : </span>&lt;input type="text" id="email-validate" maxlength="60" value="&lt;?=set_value("email-validate")?&gt;" /&gt;&lt;/li>
    <li>&lt;input type="submit" id="register" value="Register" /&gt;&lt;/li>
</ul>
&lt;input type="hidden" name="isPostBack" value="true" /&gt;
</fieldset>
&lt;/form&gt;
</div>

When i test this code, it never validates even if you fill the all fields. It is displayed with form errors that saying "field is required". Also all fields become empty so set_value function does not work.

I couldn't find the problem. Please guide me to find the solution.
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Form controls are submitted by name, not id. You need to add the name attribute to your form controls. If you dump the post array, you'll see that none of your data is in there. That also explains why your form inputs are not being repopulated.

Hope this helps.
#3

[eluser]jedd[/eluser]
You might also enjoy the benefits of using the [url="http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html"]CI Form Helper[/url]
#4

[eluser]RickRaven[/eluser]
well... how i missed such a simple thing. sure i probably never find the problem myself because of this simplicity.

thank you for the help TheFuzzyOne...

and jedd... i will check it soon. thank you too.




Theme © iAndrew 2016 - Forum software by © MyBB