回调功能,用于在配置文件中进行表单验证 [英] Callback function for form validation in config file

查看:70
本文介绍了回调功能,用于在配置文件中进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行电子邮件验证,由此电子邮件的域将为 @ abc123.com 。我已经将表单验证规则分离到 application / config 文件夹中的另一个文件中,该文件名为 form_validation.php 。我的规则之一包括 callback_email_check

I'm trying to do an email validation whereby the domain of the email would be @abc123.com. I've separated my form validation rules into another file in the application/config folder called form_validation.php. One of my rules consists of a callback_email_check.

该函数应该放在哪里?在主控制器中还是与 form_validation.php 文件一起,所有我的表单验证规则都在哪里?我尝试同时使用这两个选项,但是在显示错误消息的地方,我得到的输出是无法访问与您的字段名称电子邮件对应的错误消息。(email_check)

Where should I put the function? In the main controller or together with the form_validation.php file where all my form validation rules are? I've tried putting at both options but at where I display my error message I'm getting an output saying Unable to access an error message corresponding to your field name Email.(email_check).

function email_check($email)
{
    if( strpos($email, '@abc123.com') !== FALSE ) return TRUE;

    $this->form_validation->set_message('email', 'Please use abc123 email only.');

    return FALSE;
}

form_validation.php

form_validation.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/* Form Validation Rules */

$config = array(
    'login' => array(
            array(
                    'field' => 'user_id',
                    'label' => 'User ID',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'password',
                    'label' => 'Password',
                    'rules' => 'trim|required'
            )
    ),
    'sign_up' => array(
            array(
                    'field' => 'user_id',
                    'label' => 'User ID',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'name',
                    'label' => 'Name',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'trim|required|valid_email|callback_email_check'
            ),
            array(
                    'field' => 'department',
                    'label' => 'Department',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'password',
                    'label' => 'Password',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'cfm_password',
                    'label' => 'Re-type Password',
                    'rules' => 'trim|required|matches[password]'
            )
    ),
    'edit_profile' => array(
            array(
                    'field' => 'new_password',
                    'label' => 'New Password',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'retype_password',
                    'label' => 'Re-type Password',
                    'rules' => 'trim|required|matches[new_password]'
            )
    ),
    'forgot_password' => array(
            array(
                    'field' => 'user_id',
                    'label' => 'User ID',
                    'rules' => 'trim|required'
            ),
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'trim|required|valid_email|callback_email_check'
            )
    )
);
?>


推荐答案

在您的函数email_check中,set_message不正确

On your function email_check, the set_message is not correct it should be the same name as the function.

更改此

$this->form_validation->set_message('email', 'Please use abc123 email only.');

$this->form_validation->set_message('email_check', 'Please use abc123 email only.');

回叫 http://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks

这篇关于回调功能,用于在配置文件中进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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