更新验证在php codeigniter中无效

时间:2014-04-17 05:34:03

标签: php sql codeigniter

我的控制器是main.php。我在控制器中有两个函数,首先是update(),然后是验证set_rules()。

    function update($id){
   $this->load->library('form_validation');
   $this->load->library('table');
    // set validation properties
    $this->set_fields();
    $this->set_rules();

    // prefill form values
    $person = $this->personmodel->get_by_id($id)->row();
    $this->form_validation->id = $id;
    $this->form_validation->name = $person->name;
     $this->form_validation->email = $person->email;
      $this->form_validation->address = $person->address;
      $this->form_validation->contact_no= $person->contact_no;


    // set common properties
    $data['title'] = 'Update person';
    $data['message'] = '';
    $data['action'] = site_url('main/set_rules');
    $data['link_back'] = anchor('main/members/','Back to list of           persons',array('class'=>'back'));

    // load view
    $this->load->view('personEdit', $data);

     function set_fields(){
 $this->load->library('form_validation');
 $fields[] = array(
'id' => 'id',
'name' => 'name',
'email' => 'email',
'address' => 'address',
'contact_no' => 'contact_no',

             );


    $this->form_validation->set_rules($fields);
}

  public function set_rules(){
  $this->load->library('form_validation');
  $this->load->library('table');


     $this->form_validation->set_rules('name','required|trim|xss_clean');
    $this->form_validation->set_rules('email',  'required|valid_email|is_unique[users.email]|trim|xss_clean|callback_validate_credentials');
     $this->form_validation->set_rules('address',  'required|_alpha_dash_space');
      $this->form_validation->set_rules('contact_no',  'required|numeric');
      $this->form_validation->set_message('is_unique', "* That email address already exists") ;
      $this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');

    $this->form_validation->set_message('required', '* required');
     $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    $this->form_validation->set_message('isset', '* required');


    // run validation
    if ($this->form_validation->run()){
                $id = $this->input->post('id');
        $person = array('name' => $this->input->post('name'),
                                    'email' => $this->input->post('email'),
                        'address' => $this->input->post('address'),
                        'contact_no' => $this->input->post('contact_no')
                                                    );
        $this->personmodel->update($id,$person);


        // set user message
        $data['message'] = '<div class="success">update person success</div>';
        redirect('main/members');
//  $data['message'] = '';

    }else{
        $this->form_validation->set_message('error in updating');
    }

我的观点:personEdit.php

             <form method="post" action="<?php echo $action; ?>">
    <div class="data">

    <table>
        <tr>
            <td width="30%">ID</td>
            <td><input type="text" name="id" disabled="disable" class="text"value="<?php echo $this->form_validation->id; ?>"/></td>
            <input type="hidden" name="id" value="<?php echo $this->form_validation->id; ?>"/>
        </tr>
        <tr>
            <td valign="top">Name<span style="color:red;">*</span></td>
            <td><input type="text" name="name" class="text" value="<?php echo $this->form_validation->name; ?>"/>
            <?php echo form_error("name"); ?></td>
        </tr>
        <tr>
                         <td valign="top">Email<span style="color:red;">*</span></td>
            <td><input type="text" name="email" class="text" value="<?php echo $this->form_validation->email; ?>"/>
            <?php echo form_error('email'); ?></td>
        </tr>
        <tr>
            <td valign="top">Address<span style="color:red;">*</span></td>
            <td><input type="text" name="address" class="text" value="<?php echo $this->form_validation->address; ?>"/>
            <?php echo form_error('address'); ?></td>
        </tr>
        <tr>
            <td valign="top">Contact No<span style="color:red;">*</span></td>
            <td><input type="text" name="contact_no" class="text" value="<?php echo $this->form_validation->contact_no; ?>"/>
            <?php echo form_error('contact_no'); ?></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Save"/></td>
        </tr>
    </table>
    </div>
    </form>

这里我的更新工作正常..但是当我在表单中输入错误时,我没有收到错误。我的验证无效。任何人都可以帮助我在这段代码中做了什么错误吗?

1 个答案:

答案 0 :(得分:0)

您的set_rules函数中没有参数,请尝试在此处添加 $ fields

public function set_rules($fields){

以及后来的

// run validation
if ($this->form_validation->run($fields)){