在codeigniter中验证max_length,min_length

时间:2011-12-13 09:44:31

标签: php codeigniter validation

尝试在CodeIgniter中为min_lengthmax_length验证限制设置自定义验证消息时出错。

这些是我的验证规则:

$this->form_validation->set_rules('username', 'Username', 'required|xss_clean|min_length[6]|max_length[10]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';

这些是我的自定义验证消息:

$this->form_validation->set_message('min_length[3]', '%s: the minimum of characters is three');
$this->form_validation->set_message('max_length[20]', '%s:the maximum of characters is 20');

在我的示例中,我们有两个字段,但是我有许多字段具有不同的最小和最大长度值。它们都有特定的约束验证。此消息不起作用。默认情况下会显示该消息。谢谢你的帮助。

4 个答案:

答案 0 :(得分:12)

只需添加另一个%s即可获得长度值..

$this->form_validation->set_message('min_length', '%s: the minimum of characters is %s');

只有在第二次使用%s 时才会给出尺寸。第一次它会给你指定的字段名称..

答案 1 :(得分:9)

$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';

缺少结束括号 - )

$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]');


更新
也许使用callback函数会更好? E.g:

$this->form_validation->set_rules('username', 'Username', 'required|xss_clean|callback__check_length[6,10]');


/**
 * Callback function. Checks if the length of the user's input
 * conforms to minimum and maximum character length requirements
 *
 * @param  string
 * @param  int
 * @param  int
 * @return bool
 */
function _check_length($input, $min, $max)
{
    $length = strlen($input);

    if ($length <= $max && $length >= $min)
    {
        return TRUE;
    }
    elseif ($length < $min)
    {
        $this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
        return FALSE;        
    }
    elseif ($length > $max)
    {
        $this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
        return FALSE;        
    }
}

答案 2 :(得分:2)

使用set_message时,您无法指定已完成的长度(最小值和最大值)。您可以为min_lengthmax_length设置常规消息,例如:

$this->form_validation->set_message('min_length', 'The value you have entered for %s is too short');
$this->form_validation->set_message('max_length', 'The value you have entered for %s is too long');

否则,你将不得不利用CodeIgniter的回调功能并设置一个自定义验证功能,看起来像这样(最小长度!!)。

public function custom_min_length($str, $val) {
    if (preg_match("/[^0-9]/", $val)) {
        return FALSE;
    }
    if (function_exists('mb_strlen')) {
        if(mb_strlen($str) < $val) {
            $this->form_validation->set_message('custom_min_length', '%s: The Minimum Number Of Characters Is ' . $val);
            return FALSE;
        } else {
            return TRUE;
        }
    }
    if(strlen($str) < $val) {
        $this->form_validation->set_message('custom_min_length', '%s: The Minimum Number Of Characters Is ' . $val);
        return FALSE;
    } else {
        return TRUE;
    }
}

您可以像这样设置验证规则:

$this->form_validation->set_rules('username', 'Username', 'callback_custom_min_length[6]|required|xss_clean');

请注意callback_规则上的custom_min_length前缀。这取代了,而不是通常的min_length CI函数。

希望有所帮助......我会告诉你如何做custom_max_length功能:)

答案 3 :(得分:0)

如果您想显示自己的验证消息,请执行以下操作:

$this->form_validation->set_message('min_length','YOUR MESSAGE');

没有分支:[num]

问候