Adding custom callback to Codeigniter Form Validation

phpvalidationformcallbackcodeignitercustomaddingfunction

Last Update : 2023-09-22 UTC 09:40:47 AM

Answers of > Adding custom callback to Codeigniter Form Validation

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')
        )
);

Current topics : Adding custom callback to Codeigniter Form Validation

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:41:15 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:41:04 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:40:50 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:40:32 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:40:15 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:40:05 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:39:53 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:39:46 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:39:30 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:39:15 PM

Top
© 2023 W3CODED - All Rights Reserved.