我正在使用自定义验证回调通过 codeigniter 执行一些表单验证。
$this->form_validation->set_rules('testPost', 'test', 'callback_myTest');
回调在模型中运行,并且如果返回值为 TRUE 或 FALSE,则按预期工作。然而,文档还说您可以返回您选择的字符串。
例如,如果我有一个经过验证的日期,但在同一函数中,日期的格式发生了更改,我将如何在 Controller 中返回并检索这个新的格式化值?
感谢您的阅读并感谢您的帮助。
最佳答案
我不完全确定我得到了你的要求,但这是一次尝试。
您可以在构造函数中定义一个函数作为回调,并在该函数中使用您的模型。像这样的事情:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controllername extends CI_Controller {
private $processedValue;
public function index()
{
$this->form_validation->set_rules('testpost','test','callback');
if ($this->form_validation->run()) {
//validation successful
echo $this->processedValue; //outputs the value returned by the model
} else {
//validation failed
}
}
private function callback($input)
{
$this->load->model('yourmodel');
$return = $this->yourmodel->doStuff($input);
//now you have the user's input in $input
// and the returned value in $return
//do some checks and return true/false
$this->processedValue = $return;
}
}
关于php - codeigniter 表单回调值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627601/