从codeigniter中删除MY_Form_validation中的规则

时间:2016-04-28 15:16:20

标签: php codeigniter

我正在写一些内容,如果您选择某个按钮或下拉列表,我想更改某个字段的规则。基本上逻辑是,选中一个按钮或从选择中选择一个ID,你不需要填充地址字段。我一直试图在form_validation中做事,而不是在控制器中我可以做这样的事情

if(isset($_POST['checkbox'])){
    //check rules
}

所以我做到了这一点:

public function check_address($str,$prefix=null){
    $this->set_message('check_address','Please select an address or fill out a new one');
    //var_dump($this->_field_data);exit;
    $remove=array('address_line1','address_line2','address_line3','city','postcode');
    if($prefix!==null){
        foreach($remove as &$r){
            $r=$prefix.'_'.$r;
        }
    }
    unset($r);
    foreach($this->_field_data as $key=>$f){
        if(in_array($key,$remove)){
            unset($this->_field_data[$key]);
        }
    }
}

这是我想要的方式,但我认为我已经干扰了codeiginter的Form_Validation类,因为它抛出一个错误,指出我的数组中所需的索引没有设置。

错误消息

A PHP Error was encountered
Severity: Notice
Message: Undefined index: billing_address_line1
Filename: libraries/Form_validation.php
Line Number: 481

我不太确定如何在不干扰codeigniter需要的情况下实现我想做的事情?是否需要删除另一个数组,以便键不能排序?

我现在也这样做了

    foreach($this->_field_data as $key=>&$f){
        if(in_array($key,$remove)){
            //unset($this->_field_data[$key]);
            //str_replace('/required\|/','',$f['rules']);
            //str_replace('/required/','',$f['rules']);
            foreach($f['rules']as$r=>$val){
                $val=strtolower($val);
                if($val=='required')unset($f['rules'][$r]);
            }
            var_dump($f);
        }
    }
    unset($f);

这现在可以通过删除所需的规则,但我认为必须已经运行的所需功能来完成所需的功能吗?

规则数组

$this->con['validation']['checkout']=array(


        array('field'=>'address_line1','label'=>'Address line 1','rules'=>'required|min_length[3]|max_length[200]|check_basket'),
        array('field'=>'address_line2','label'=>'Address line 2','rules'=>'min_length[3]|max_length[200]'),
        array('field'=>'address_line3','label'=>'Address line 3','rules'=>'min_length[3]|max_length[200]'),
        array('field'=>'city','label'=>'Town/City','rules'=>'required|min_length[3]|max_length[50]'),
        array('field'=>'postcode','label'=>'Town/City','rules'=>'required|min_length[3]|max_length[9]|valid_postcode'),

        array('field'=>'shipping_addressID','label'=>'Address','rules'=>'check_address[]'),


        array('field'=>'billing_address_line1','label'=>'Billing address line 1','rules'=>'required|min_length[3]|max_length[200]'),
        array('field'=>'billing_address_line2','label'=>'Billing address line 2','rules'=>'min_length[3]|max_length[200]'),
        array('field'=>'billing_address_line3','label'=>'Billing address line 3','rules'=>'min_length[3]|max_length[200]'),
        array('field'=>'billing_city','label'=>'Town/City','rules'=>'required|min_length[3]|max_length[50]'),
        array('field'=>'billing_postcode','label'=>'Town/City','rules'=>'required|min_length[3]|max_length[9]|valid_postcode'),
        array('field'=>'billing_address_same','label'=>'Billing Address','rules'=>'check_address[billing]'),
        array('field'=>'billing_addressID','label'=>'Billing address','rules'=>'check_address[billing]')
    );

2 个答案:

答案 0 :(得分:0)

我认为最简单的方法是最好的。在控制器中,如果选中该复选框,则添加规则。

if(isset($_POST['checkbox'])){
    $this->form_validation->set_rules('billing_address_line1', 'Billing Address Line 1', 'required');
}

在评论和问题编辑后添加

仍在努力实现最简单的实现,同时避免使用一堆字符串操作的foreach循环,以及几乎相同的多个规则数组。

这利用了form_validation->set_rules()可以接受第三个参数中的数组(而不是管道分隔的字符串)的事实。无论如何,字符串最终会变成一个数组,因此从数组开始在运行时更有效。

同样以高效运行时的名义,在设置规则时使用方法链接。

首先创建可重复使用的"规则"要传递给set_rules()的数组。

$rules_address1 = ['required', 'min_length[3]', 'max_length[200]', 'callback_check_basket'];
$rules_address_23 = ['min_length[3]', 'max_length[200]'];
$rules_city = ['required', 'min_length[3]', 'max_length[50]'];
$rules_postcode = ['required', 'min_length[3]', 'max_length[9]', 'callback_valid_postcode'];

$this->form_validation->set_message('required', 'You must provide {field}.');

$this->form_validation
    ->set_rules('address_line1', 'Address line 1', $rules_address1)
    ->set_rules('address_line2', 'Address line 2', $rules_address_23)
    ->set_rules('address_line3', 'Address line 3', $rules_address_23)
    ->set_rules('city', 'Town/City', $rules_city)
    ->set_rules('postcode', 'Postal Code', $rules_postcode);

if(!isset($_POST['checkbox']))
{
  unset($rules_address1[0]);
  unset($rules_address_23[0]);
  unset($rules_city[0]);
  unset($rules_postcode[0]);
}

$this->form_validation
    ->set_rules('billing_address_line1', 'Billing Address line 1', $rules_address1)
    ->set_rules('billing_address_line2', 'Billing Address line 2', $rules_address_23)
    ->set_rules('billing_address_line3', 'Billing Address line 3', $rules_address_23)
    ->set_rules('billing_city', 'Town/City', $rules_city)
    ->set_rules('billing_postcode', 'Postal Code', $rules_postcode);

我跳过addressID字段的规则,因为我不确定它是如何使用的。

此外,根据CI SOP,将callback_添加到我认为是自定义回调方法的内容中。相应调整。

如您所知,这一切都发生在调用$this->form_validation->run()之前的控制器中。

答案 1 :(得分:0)

从表单验证类中检查它。为要删除的规则调用自己的验证方法。 https://codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

然后在这些方法中,检查是否选中了Checkbox。如果选中,则跳过所有规则,并从回调函数返回TRUE。像这样:

address_line1_callback_function() {
    CI = &get_instance();
    if ( CI->input->post('checked') ) 
        return true;
    /// Rules for address line go here
}