CodeIgniter:如何从表单验证回调返回数据 [英] CodeIgniter: how to return data from a form validation callback

查看:122
本文介绍了CodeIgniter:如何从表单验证回调返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的CI表单验证规则:

Here is my CI form validation rule:

$datetime_string = $this->form_validation->set_rules('event_date', 'Select', 'callback_date_validate');

这是我的回呼:

function date_validate($select_value)
{
    $year = '';
    $month = '';
    $day = '';
    $hour = '';
    $minutes = '';

    $datetime = $this->input->post('event_date');
    if (strpos($datetime, ' @ ') !== 'FALSE' && $datetime != '')
    { 
        $datetime_explode = explode(' @ ', $datetime);
        if (strpos($datetime_explode[0], '/') !== 'FALSE' && $datetime_explode != '')
        {
            $date_explode = explode('/', $datetime_explode[0]);
            $year = $date_explode[2];
            $month = $date_explode[1];
            $day = $date_explode[0];    
        }
        if (strpos($datetime_explode[1], ':') !== 'FALSE')
        {
            $time_explode = explode(':', $datetime_explode[1]);
            $hour = $time_explode[0];
            if (strpos($time_explode[1], ' ') !== 'FALSE')
            {
                $minutes_explode = explode(' ', $time_explode[1]);
                $minutes = $minutes_explode[0];
                $am_pm = $minutes_explode[1];

                if ($am_pm == 'PM' || $am_pm == 'pm')
                    $hour += 12;    
            }
        }
    }

    $datetime_string = $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minutes . ':00';

    if (!preg_match('/^\d{4}-\d{2}-\d{2} 2[0-3]|[01][0-9]:[0-5][0-9]:[0-5][0-9]$/', $datetime_string))
    {
        $this->form_validation->set_message('date_validate', 'Oops');
    }
    else // user picked something
    {
        return $datetime_string;
    }
}

根据CI文档,一个表单验证回调,但通过设置规则等于一个变量,我得到这个错误:

According to the CI documentation, you can return data from a form validation callback, but by setting the rule equal to a variable, I get this error:

Object of class CI_Form_validation could not be converted to string

我做错了什么?

推荐答案

我想问题出在这一行:

$datetime = $this->input->post('event_date');

event_date字段的值在函数$ select_value的参数中捕获,请尝试使用您的参数,而不是发布数据。

The value of event_date field is captured in a parameter of your function $select_value try using your parameter instead of post data.

这篇关于CodeIgniter:如何从表单验证回调返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆