dongshungai4857 2015-03-19 06:27
浏览 47

Codeigniter表单验证将不会运行

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.

  • 写回答

1条回答 默认 最新

  • dongmeng2509 2015-03-19 21:03
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64