簡體   English   中英

如何在codeigniter中為多個文本字段使用相同的回調函數

[英]How to use same callback function for multiple text fields in codeigniter

我想檢查值是否不是整數和浮點數,我已經寫了表格驗證如下,

$this->form_validation->set_rules('charge_hour', 'Per hour', 'xss_clean|callback_money_type');
$this->form_validation->set_rules('charge_day', 'Per day', 'xss_clean|callback_money_type');
$this->form_validation->set_rules('charge_weekly', 'Per week', 'xss_clean|callback_money_type');
$this->form_validation->set_rules('charge_monthly', 'Per month', 'xss_clean|callback_money_type');

所有文本money_type()常用回調函數是money_type()

    public function money_type($charge)
        {
            if (is_float($charge) == false && is_int($charge) == false && $charge >= 0)
            {
                $this->form_validation->set_message('{WHAT TO ENTER HERE}', 'Enter valid charges for space.');
                return FALSE;
            }
        else
        {
            return TRUE;
        }
    }

如何在驗證期間找出{輸入什么內容}? 字段名稱是運行時的charge_hour,charge_day,charge_weekly,charge_monthly? 這樣表單驗證將為每個字段顯示不同的錯誤消息。

謝謝。

您可以在回調參數中傳遞文件名

$this->form_validation->set_rules('charge_hour', 'Per hour', 'xss_clean|callback_money_type[charge_hour]');
$this->form_validation->set_rules('charge_day', 'Per day', 'xss_clean|callback_money_type[charge_day]');
$this->form_validation->set_rules('charge_weekly', 'Per week', 'xss_clean|callback_money_type[charge_weekly]');
$this->form_validation->set_rules('charge_monthly', 'Per month', 'xss_clean|callback_money_type[charge_monthly]');

你回調函數

public function money_type($charge,$fild_name)
        {
            if (is_float($charge) == false && is_int($charge) == false && 

    $charge >= 0)
                {
                    $this->form_validation->set_message("{$fild_name}", 'Enter valid charges for space.');
                    return FALSE;
                }
            else
            {
                return TRUE;
            }
        }

我得到了答案,我的錯誤在這里。

{WHAT TO ENTER HERE}應該與回調函數名稱function money_type ,那么它將適用於所有字段回調

$ this-> form_validation-> set_message('{輸入什么內容}','輸入有效的空間費用。');

應該

$ this-> form_validation-> set_message('money_type','輸入有效的空間費用。');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM