A- Create a callback validation function in w3coded callback adding the same controller,In this example, we will w3coded callback adding suppose you have a form with this w3coded callback adding inputs,2-Create Codeigniter validation callback w3coded callback adding function,3-Add Custom callback function in w3coded callback adding validation rules
In addition, we add our callback function in the end to validate that user will submit phone number.
<form role="form" class="form-horizontal" action="<?= base_url() ?>contactus/send" method="post">
<?php
if ($this->session->flashdata('success_msg')) {
echo "<p class='notice succeed'>" . $this->session->flashdata('success_msg') . "</p>";
}?>
<div class="form-group">
<input type="text" name="co_name" value="<?= set_value('co_name') ?>" class="form-control" placeholder="Name"/></div>
<?php echo form_error('co_name'); ?>
<div class="form-group">
<input name="co_email" value="<?= set_value('co_email') ?>" type="email" class="form-control" placeholder="Email "/></div>
<div class="form-group">
<input class="form-control" type="text" name="co_phone" placeholder=" Phone"/>
</div>
<?php echo form_error('co_title'); ?>
<button type="submit" class="btn btn-primary"> Send </button>
</form>
In our example, we will create it like this
function validate_phone_num($input){
return ( ! preg_match("/^[0-9*#+]+$/", $input)) ? FALSE : TRUE;
}
$this->form_validation->set_rules('cj_phone', 'Phone' ,'trim|required|xss_clean|callback_validate_num|htmlspecialchars');
Now after validating your input against regex function you can put the custom error message to the Send function, this message will trigger if callback function return False.
$this->form_validation->set_message('validate_num', 'The {field} doesn’t contain a correct phone number);
The full send function will be like this
function send()
{
$this->form_validation->set_rules('co_name', $this->lang->line('name'), 'trim|required|xss_clean|htmlspecialchars');
$this->form_validation->set_rules('co_email',$this->lang->line('email'), 'trim|required|xss_clean|valid_email|htmlspecialchars');
$this->form_validation->set_rules('co_phone', 'Phone' ,'trim|required|xss_clean|callback_validate_num|htmlspecialchars');
$this->form_validation->set_message('co_phone', 'The {field} doesn’t contain a correct phone number);
if($this->form_validation->run()== false){
$this->view('site/contactus');
}else {
$this->contactus->send();
$data['msg'] = "Sent Successfully";
$this->view('theme/msg2',$data);
}
}
But what if we want to use a function from another model in our application is this allowed, yes you can do it like this:
$this->form_validation->set_rules(
'co_phone', 'Phone',
array(
'required',
array($this->contact_model, 'valid_phone')
)
);
Last Update : 2023-09-22 UTC 13:41:04 PM
Last Update : 2023-09-22 UTC 13:40:50 PM
Last Update : 2023-09-22 UTC 13:40:32 PM
Last Update : 2023-09-22 UTC 13:40:15 PM
Last Update : 2023-09-22 UTC 13:40:05 PM
Last Update : 2023-09-22 UTC 13:39:30 PM
Last Update : 2023-09-22 UTC 13:39:15 PM