代码点击器中不同条件的表单验证 [英] form validation for different condition in code igniter

查看:177
本文介绍了代码点击器中不同条件的表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个表单用于两个函数/条件:保存(添加)和更新
现在我设置一个规则到我的输入文本: $ this-> form_validation- ; set_rules('ID_user','User ID','required');



逻辑如下:保存条件然后我的输入文本将被启用,因为他们将tyoe他们的id,但如果用户处于更新条件,则我的输入文本将被禁用,文本将是当前用户id,因为他们不能改变他们的用户id。 p>

这是我的控制器中的保存(添加)和更新函数:

  function add(){
//设置公共属性
$ data ['title'] ='Tambah User baru';
$ data ['action'] = site_url('user / add');
$ data ['link_back'] = anchor('user / index /','返回用户列表',array('class'=>'back'));

$ this-> _set_rules();
//运行验证
if($ this-> form_validation-> run()== false){
$ data ['message'] ='';
// bedakan add / update
$ data ['validate'] ='add';
//设置公共属性
$ data ['title'] ='添加新用户';
// $ 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 {
//保存数据
$ user = array('ID_user'=> $ this-> input-> post ID_user'),
'pass'=> sha1($ this-> input-> post('pass')),
'nama'=> $ this-& > post('nama'),
'email'=> $ this-> input-> post('email'),
'active'=> $ this-> input'> post('active'),
'regis_date'=> date('Ymd H:i:s'

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

//设置表单输入nama =id
$ this-> validation-> ID_user = $ ID_user;

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

函数更新($ ID_user){
//设置公共属性
$ data ['title'] ='更新用户' ;
$ this-> load-> library('form_validation');

//设置验证属性
$ this-> _set_rules();
$ data ['action'] =('user / update /'.ID ID_user);

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

//运行验证
if($ this-> form_validation-> run()== false){
$ data ['message'] ='';

$ data ['user'] = $ this-> user_m-> get_by_id($ ID_user) - > row_array();
//设置公共属性
$ data ['title'] ='更新用户';
$ data ['message'] ='';
}
else {
//保存数据
$ ID_user = $ this-> input-> post('ID_user');
$ user = array(
'pass'=> $ this-> input-> post('pass'),
'nama'=> $ this-> input> post('email'),
'active'=> $ this-> > input-> post('active'),
'regis_date'=> date('Ymd H:i:s'))

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

//设置用户消息
$ data ['message'] ='更新用户成功!
}

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

这是视图中的输入类型:

 < input type =textname =ID_userclass =text
<?php if($ validate! add'){echodisabled;}?>
value =<??php echo(isset($ user ['ID_user']))?$ user ['ID_user']:?

问题是:当我的用户处于更新条件时,错误消息显示,因为php认为userid为null,事实是用户id已经存在,我所做的只是打印用户id在我的输入类型和禁用它
这是错误消息:需要用户ID字段。

解决方案

用户ID字段作为更新表单中的隐藏字段,以确保其与其余表单字段一起提交。

 < input type =hiddenname =user [ID_user]value =<?= $ user [ 'ID_user']?> /> 

只要确保以某种方式验证当前登录的用户,以确保没有猴子的另一个用户的记录。


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.

解决方案

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.

这篇关于代码点击器中不同条件的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆