I'm working on a project which uses CodeIgniter 3.1.11 version.
Currently, I'm working on a form where I need to validate all the fields in the form except one which is optional. I see CodeIgniter 3 is validating all the fields every time I'm submitting this form. Below is the code for my form.
<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>
Below is my controller file
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
}
}
I'm trying to skip the validation for the phone field. I've found in my research that in CodeIgniter 4, the below line would make the phone field optional but it's not working on CodeIgniter 3.1.11. I've researched various articles but not able to find any relevant answer for this. Can someone please suggest me about this ?
$this->form_validation->set_rules('phone', 'Phone', 'permit_empty');
Can someone please help me with this ?
CodePudding user response:
For more control don't use ->set_rules('field', 'label', 'rules')
. Use array:
$config = [
[
'field' => '',
'label' => '',
'rules' => '',
],
...
];
$this->form_validation->set_rules($config);
So define your general array and when you use phone, just add it to array.