1084

Custom Validation Callback function is not being triggered

Showing 1 to 10 of 14 comments.

Comments for “Custom Validation Callback function is not being triggered”
 

Posted by eleetgeek on Tuesday 26th April 2022 at 10:00 GMT

Custom validation callback function is not being triggered by set_rules() function.

Am i missing something?

function index() {
        $submit = post('submit');

        if ($submit == 'Submit') {
            $this->validation_helper->set_rules('email', 'Email', 'required|valid_email|callback_otp_custom');

            //run the validation tests
          $result = $this->validation_helper->run();

          if ($result == true) {
              //echo 'well done';
               $this->_generate_otp(post('email'));
          } else {
                validation_errors();
            }
       }
       
        $this->view('login');
    }
    
    function otp_custom($email)
 {
       $error_msg = "Email not found in system. Please contact admin.";
      return $error_msg;
      die();
  }

eleetgeek

User Level: Guest

Date Joined: 27/07/2021

Posted by DaFa on Tuesday 26th April 2022 at 23:45 GMT

Hi there,

The callback is working but the reason you aren't seeing it is you are loading your view over the error msg and you're not setting any rules so you will always get a validation error_msg with the code you shared.

move validation_errors(); into your view
<h1><?= $headline ?></h1>
<?= validation_errors() ?>
<div class="card">
    <div class="card-heading">
        User Details
    </div>
    <div class="card-body">
        <?php
        echo form_open($form_location);
        echo form_label('Name');
        echo form_input('name', $name, array("placeholder" => "Enter Name"));
        echo form_label('Email');
        echo form_input('email', $email, array("placeholder" => "Enter Email"));
        echo form_submit('submit', 'Submit');
        echo anchor($cancel_url, 'Cancel', array('class' => 'button alt'));
        echo form_close();
        ?>
    </div>
</div>

Try this for fun and enter '[email protected]' as an email
    function opt_custom($email) {
        if ($email == '[email protected]') {
            $error_msg = 'Sorry no eleetgeek emails allowed here!';
            return $error_msg;
        } else {
            return true;
        }
    }
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by eleetgeek on Wednesday 27th April 2022 at 04:37 GMT

No that is not the case. "required | valid_email" is working fine and giving error if needed, only "callback_" function is not giving error message.

Please check below POC.

VIEW FILE



Email





CONTROLLER FILE

    function index() {
     $submit = post('submit');
     if ($submit == 'Submit') {
            $this->validation_helper->set_rules('email', 'Email', 'required|valid_email|callback_otp_custom');

            //run the validation tests
          $result = $this->validation_helper->run();

          if ($result == true) {
              echo 'well done';
         } else {
                //validation_errors();
          }
       }
        $this->view('welcome');
    }
 
    function otp_custom($email)
 {
       if ($email == '[email protected]') {
            $error_msg = 'Sorry no eleetgeek emails allowed here!';
            return $error_msg;
        } else {
            return true;
        }
    }
}


Edit: I don't know why my view file is being truncated while posting here, please check below link.
View file:- https://justpaste.it/6i2w9

This comment was edited by eleetgeek on Wednesday 27th April 2022 at 04:51 GMT

eleetgeek

User Level: Guest

Date Joined: 27/07/2021

Posted by eleetgeek on Wednesday 27th April 2022 at 06:28 GMT

Found the problem and raised a issue on github: https://github.com/davidjconnelly/trongate-framework/issues/88

eleetgeek

User Level: Guest

Date Joined: 27/07/2021

Posted by DaFa on Wednesday 27th April 2022 at 06:49 GMT

Hi eleetgeek,

When pasting your code, you have to replace all HTML entity tags, see https://www.w3schools.com/html/html_entities.asp

Like this for your welcome.php view file, which you have called 'login' above
<!DOCTYPE html>
<html>

<body>

  <h2>Custom Validation Test Trongate!</h2>

  <form method='POST'>
    <?= validation_errors() ?>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" value="">
    <input type="submit" name="submit" value="Submit">
    <?= form_close() ?>

</body>

</html>


Your controller, view (form) and submit (method) is not correctly setup. I'd suggest you create a module with the Desktop app to see how it would be better structured.

However let's see if we can get your code working.

Your controller should look like this:
    function index() {
        $submit = post('submit');

        if ($submit == 'Submit') {
            $this->validation_helper->set_rules('email', 'Email', 'required|valid_email|callback_otp_custom');

            //run the validation tests
            $result = $this->validation_helper->run();

            if ($result == true) {
                echo 'well done';
                //$this->_generate_otp(post('email'));
            }
        }

        $this->view('welcome');
    }

    function otp_custom($email) {
        $error_msg = "Email not found in system. Please contact admin.";
        return $error_msg;
    }


As I said above, the custom call back needs to return 'true' or an 'error_msg' to be useful, otherwise it will always return an error_msg as above.

Change your callback to this and enter '[email protected]' for an error_msg and anything else for a 'well done' response
    function opt_custom($email) {
        if ($email == '[email protected]') {
            $error_msg = 'Email not found in system. Please contact admin.';
            return $error_msg;
        } else {
            return true;
        }
    }
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by eleetgeek on Wednesday 27th April 2022 at 08:46 GMT

I understand what you are trying to explain.

Problem is callback_function is not getting trigger if "view endpoint" and "form action endpoint" is same. In this case inbuilt set_rules() are working fine, just callback_function is not working (always returns true or not being trigger)

eleetgeek

User Level: Guest

Date Joined: 27/07/2021

Posted by djnordeen on Wednesday 27th April 2022 at 10:24 GMT

It looks like you have different names.
You are trying to call callback_otp_custom
Your function is otp_custom

Updated
Sorry, did not see that
https://trongate.io/docs_m/information/custom-validation-callbacks

Dan

This comment was edited by djnordeen on Wednesday 27th April 2022 at 11:25 GMT

Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by eleetgeek on Wednesday 27th April 2022 at 10:37 GMT

Syntax is correct, please check the documentation https://trongate.io/docs_m/information/custom-validation-callbacks

eleetgeek

User Level: Guest

Date Joined: 27/07/2021

Posted by eleetgeek on Wednesday 27th April 2022 at 13:20 GMT

Attaching a POC: https://www.mediafire.com/file/kic6qln7ykuooa0/custom_validation_test.zip/file

Please have a look.

eleetgeek

User Level: Guest

Date Joined: 27/07/2021

Posted by DaFa on Wednesday 27th April 2022 at 15:10 GMT

Thanks for sharing that zip file, it helped a lot and I have the solution.

The problem is you were testing the validation helpers callback_ from the Welcome controller, which when I looked into the validation_helper.php 'attemp_invoke_callback() it was trying to get the module name from the first segment, which is null and I was confused because I was testing it from a new module.

This doesn't work
http://localhost/custom_validation_test/

This does, as 'test_validation' is segment(1)
http://localhost/custom_validation_test/test_validation


The patch to make it work without a segment(1), index of the default Controller is right after line 457 of the 'validation_helper.php'
$target_module = ucfirst($this->url_segment(1));

add this to line 458
            // use default controller if there is no segment(1)
            if ($target_module == NULL) {
                $target_module = DEFAULT_CONTROLLER;


attempt_invoke_callback() should now look like this

private function attempt_invoke_callback($key, $label, $posted_value, $test_to_run) {

$chars = substr($test_to_run, 0, 9);

if ($chars == 'callback_') {
$target_module = ucfirst($this->url_segment(1)); // use default controller if there is no segment(1) if ($target_module == NULL) { $target_module = DEFAULT_CONTROLLER; }
$target_method = str_replace('callback_', '', $test_to_run);

if (!class_exists($target_module)) {
$modules_bits = explode('-', $target_module);
$target_module = ucfirst(end($modules_bits));
}

if (class_exists($target_module)) {

$static_check = new ReflectionMethod($target_module, $target_method);
if ($static_check->isStatic()) {
// STATIC METHOD
$outcome = $target_module::$target_method($posted_value);
} else {
// INSTANTIATED
$callback = new $target_module;
$outcome = $callback->$target_method($posted_value);
}

if (gettype($outcome) == 'string') {
$outcome = str_replace('{label}', $label, $outcome);
$this->form_submission_errors[] = $outcome;
}
}
}
}
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

×