如何防止在 codeigniter 3.1.11 中验证可选字段

How to prevent an optional field from being validated in codeigniter 3.1.11

我正在开发一个使用 CodeIgniter 3.1.11 版本的项目。

目前,我正在处理一个表单,我需要在其中验证表单中的所有字段,可选字段除外。每次我提交此表单时,我都看到 CodeIgniter 3 正在验证所有字段。下面是我的表单代码。

<form action="/save_shipping_profile" method="POST" id="shipping_profile">

        <table cellPadding="15" width="100%">
            
            
            <tr>
                <td width="150">Address:</td>
                <td>
                    <input id="address" class="experian_address_autocomplete" type="text" name="address1" 
                    value="<?= $shippingInfo['shipping_address']; ?>" size="50" required /> 
                    <span class="required">*</span>
                </td>
            </tr>

            <tr>
                <td width="150">Address 2<br /><span style="color:#a6a6a6;font-size:10px;">(Apt, Suite #, etc..)</span></td>
                <td><input id="address2" type="text" name="address2" value="<?= $shippingInfo['shipping_address2']; ?>" size="50" autocomplete="address-line2" /></td>
            </tr>

            <tr>
                <td width="150">City:</td>
                <td><input id="city" type="text" name="city" value="<?= $shippingInfo['shipping_city']; ?>" size="50" required autocomplete="address-level2" /> <span class="required">*</span></td>
            </tr>

            <tr>
                <td width="150">State / Province:</td>
                <td>
                        <input id="state" type="text" name="state" value="<?= $shippingInfo['shipping_state']; ?>" size="50" />";
                </td>
            </tr>

            <tr>
                <td width="150">Zip / Post Code:</td>
                <td><input id='zip' type="text" name="zip" value="<?= $shippingInfo['shipping_zip']; ?>" size="15" required autocomplete="postal-code" /> <span class="required">*</span></td>
            </tr>
            
            **<?php if ($site_options['show_phone_number_shipping_profile']): ?>
                <tr>
                    <td width="150">Phone Number:</td>
                    <td><input id='phone' type="tel" name="phone" value="<?= $shippingInfo['billing_phone']; ?>" size="15" autocomplete="phone-number" /></td>
                </tr>
            <?php endif; ?>**
            

            <tr>
                <td width="150">&nbsp;</td>
                <td><input id="save_shipping" type="submit" name="submit" value="Save Shipping Information" /></td>
            </tr>

        </table>
    </form>

下面是我的控制器文件

    function save_shipping_profile()
    {
        $this->load->library('form_validation');
    
        $this->form_validation->set_message('address_check', 'The %s field may not be an address.');
        $this->form_validation->set_rules('address1', 'Address', 'required|trim|xss_clean|callback_address_check');
      // $this->form_validation->set_rules('phone', 'Phone', 'permit_empty');
    
        if(!$this->form_validation->run())
        {
            $array = array();
            $array['error'] = '1';
            $array['message'] = validation_errors("- "," ");
    
        }
        else
        {
            // Execute the main code
        

        }
    }

我正在尝试跳过 phone 字段的验证。我在我的研究中发现,在 CodeIgniter 4 中,下行将使 phone 字段成为可选字段,但它不适用于 CodeIgniter 3.1.11。我研究过各种文章,但找不到任何相关的答案。有人可以就此向我提出建议吗?

$this->form_validation->set_rules('phone', 'Phone', 'permit_empty');

有人可以帮我解决这个问题吗?

要获得更多控制,请不要使用 ->set_rules('field', 'label', 'rules')。使用数组:

$config = [
  [
    'field' => '',
    'label' => '',
    'rules' => '',
  ],
  ...
];
$this->form_validation->set_rules($config);

所以定义你的通用数组,当你使用phone时,只需将它添加到数组中。