我正在開發一個使用 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"> </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
}
}
我正在嘗試跳過電話欄位的驗證。我在我的研究中發現,在 CodeIgniter 4 中,下面的行將使電話欄位成為可選,但它不適用于 CodeIgniter 3.1.11。我研究了各種文章,但找不到任何相關的答案。有人可以建議我嗎?
$this->form_validation->set_rules('phone', 'Phone', 'permit_empty');
有人可以幫我解決這個問題嗎?
uj5u.com熱心網友回復:
要獲得更多控制,請不要使用->set_rules('field', 'label', 'rules'). 使用陣列:
$config = [
[
'field' => '',
'label' => '',
'rules' => '',
],
...
];
$this->form_validation->set_rules($config);
所以定義你的通用陣列,當你使用手機時,只需將它添加到陣列中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359567.html
下一篇:按ID洗掉JSON[]陣列元素
