简体   繁体   中英

Codeigniter form validation will not run

It seems that there are may questions about form and form validation.

I've been having a problem getting my form validation to work. I created a sign up form that ask the user for various information. Then I created a form that will allow the user to edit his information. The form validation works perfectly on the sign up form, but I can't get it run the validation on the other form.

My view

<h1>Edit user</h1>

<div id="body">
        <p>Edit user information.</p>

    <?php 



    echo form_open('user_admin/user_update');

    echo validation_errors();

    echo form_hidden('id', $results->id);

    echo "<p><lable>Email:</lable>";
    echo form_input('email', $results->email);
    echo "</p>";


    echo "<p><lable>Name:</lable>";
    echo form_input('name', $results->name);
    echo "</p>";

    echo "<p><lable>Last name:</lable>";
    echo form_input('lastname', $results->lastname);
    echo "</p>";

    echo "<p><lable>Home address:</lable>";
    echo form_textarea('homeaddress', $results->homeaddress);
    echo "</p>";

    echo "<p><lable>Postal address:</lable>";
    echo form_textarea('posteladdress', $results->posteladdress);
    echo "</p>";

    echo "<p><lable>Mobile number:</lable>";
    echo form_input('mobile', $results->mobile);
    echo "</p>";

    echo "<p><lable>Home telephone:</lable>";
    echo form_input('hometel', $results->hometel);
    echo "</p>";

    echo "<p><lable>ID number:</lable>";
    echo form_input('idnum', $results->idnum);
    echo "</p>";

    echo "<p>";
    echo form_submit('edit_submit', 'Update');
    echo "</p>";

    echo form_close();

    ?> 

My controller

 public function user_update()
{
    $this->load->library('form_validation');
    $this->load->model('model_users');

    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]');
    $this->form_validation->set_rules('name', 'Name', 'required|trim');
    $this->form_validation->set_rules('lastname', 'Last name', 'required|trim');
    $this->form_validation->set_rules('homeaddress', 'Home address', 'required|trim');
    $this->form_validation->set_rules('posteladdress', 'Postel address', 'required|trim');
    $this->form_validation->set_rules('mobile', 'Mobile number', 'required|trim');
    $this->form_validation->set_rules('hometel', 'Home telphone', 'required|trim');
    $this->form_validation->set_rules('idnum', 'ID Number', 'required');


    $this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.');

    if ($this->form_validation->run()==true)
    {

        $id = $this->input->post('id');
        $this->model_users->update_info();
        echo "The users details have been updated successfully";
        redirect ('user_admin/user_main');
    }
    else
    {
        echo "The users details were not updated. Please contact the admistrator!";


    }


}

With the controller in its current configuration I can get the database to update by changing the if statement to FALSE. So I now that the post information is passed to the database and updated.

Please could somebody look at my code a tell me what I've done wrong.

Thanks.

Try taking this line out of the code and then re try.

$this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.');

I think this line is supposed to go into your own validation function. If it does work, then just read up on custom form validation functions on the codeigniter site.

If you are using HMVC, then that can cause some issues with custom functions in validation, but there quite easy ways around it, but you would have to Google for the exact answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM