form validation for different condition in code igniter

拈花ヽ惹草 提交于 2019-12-25 02:58:37

问题


So i have a form that used for two functions/conditions : Save(add) and Update Now i set a rule to my input text : $this->form_validation->set_rules('ID_user', 'User ID', 'required');

The logic is like this: if the user is in Save condition then my input text will be enable because they will tyoe their id, but if the user is in Update condition then my input text will be disable and the text will be the current user id because they cant change their user id.

This is the Save(add) and update functions in my controller :

function add(){
    //set common properties 
    $data['title'] = 'Tambah User baru';
    $data['action'] = site_url('user/add');
    $data['link_back'] = anchor('user/index/', 'Back to User list', array('class'=>'back'));

    $this->_set_rules();
    //run validation
    if($this->form_validation->run() == false){
        $data['message'] = '';
        //bedakan add/update
        $data['validate'] = 'add';
        //set common properties 
        $data['title'] = 'Add new User';
        //$data['message'] = '';
        $data['user']['ID_user'] = '';
        $data['user']['pass'] = '';
        $data['user']['nama'] = '';
        $data['user']['email'] = '';
        $data['user']['active'] = '';
        $data['link_back'] = anchor('user/index/', 'Lihat daftar User', array('class'=>'back'));

        $this->load->view('user_form_v', $data);
    }

    else{
        //save data
        $user = array('ID_user'=>$this->input->post('ID_user'),
        'pass'=>sha1($this->input->post('pass')),
        'nama'=>$this->input->post('nama'),
        'email'=>$this->input->post('email'),
        'active'=>$this->input->post('active'),
        'regis_date'=>date('Y-m-d H:i:s'));

        $ID_user = $this->user_m->save($user);

        //set form input nama = "id"
        $this->validation->ID_user = $ID_user;

        redirect('user/index/add_success');
    }
}

function update($ID_user){
    //set common properties
    $data['title'] = 'Update user';
    $this->load->library('form_validation');

    //set validation properties
    $this->_set_rules();
    $data['action'] = ('user/update/'.$ID_user);

    //bedakan add/update
    $data['validate'] = 'update';

    //run validation
    if ($this->form_validation->run() == false){
        $data['message'] = '';

        $data['user'] = $this->user_m->get_by_id($ID_user)->row_array();
        //set common properties
        $data['title'] = 'Update User';
        $data['message'] = '';
    }
    else{
        //save data
        $ID_user = $this->input->post('ID_user');
        $user = array(
        'pass'=>$this->input->post('pass'),
        'nama'=>$this->input->post('nama'),
        'email'=>$this->input->post('email'),
        'active'=>$this->input->post('active'),
        'regis_date'=>date('Y-m-d H:i:s'));

        $this->user_m->update($ID_user, $user);
        $data['user'] = $this->user_m->get_by_id($ID_user)->row_array();

        //set user message
        $data['message'] = 'Update User Success!';
    }

    $data['link_back'] = anchor('user/index/', 'Lihat daftar user', array('class'=>'back'));
    //load view
    $this->load->view('user_form_v', $data);
}

And this is the input type in view :

<input type="text" name="ID_user" class="text"
                <?php if($validate!='add'){echo "disabled";} ?>
                value="<?php echo (isset($user['ID_user']))?$user['ID_user']:""?>"/>

The question is : while my user in the Update condition and they want to Update, the error message is shown because php think that the userid is null, the fact is the user id is already there, all i do is just print the userid in my input type and disabled it This is the error message : The User ID field is required.


回答1:


You could include the User ID field as a hidden field in the update form to be sure that it gets submitted with the rest of the form fields.

<input type="hidden" name="user[ID_user]" value="<?=$user['ID_user']?>" />

Just make sure to validate it somehow against the current logged in user to ensure nobody monkeys with it and changes another user's record.




回答2:


You should run two different rules - once for 'saving' and one for 'updating'.

Then in the saving rule, include the rule for the user id.

In the update rule, dont include the user id.

Putting the user id into a 'hidden' field on your form is not optimal - because someone can 'edit' the page and change the hidden field to something else - thus opening up your application to be manipulated



来源:https://stackoverflow.com/questions/12329534/form-validation-for-different-condition-in-code-igniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!