CodeIgniter - 唯一值 - 错误显示 [英] CodeIgniter - Unique Value - Error Display

查看:116
本文介绍了CodeIgniter - 唯一值 - 错误显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的表单,订阅电子报,其中包含两个输入:电子邮件城市。我在表单验证中使用 is_unique ,但是此错误显示为文本。我需要在警告框或警告中修改此文本显示。需要在设计中修正此错误,建议。

I made a simple form, a newsletter subscription, which has two inputs: email and city. I use is_unique in form validation, but this error shows as text. I need to modify this text show in an alert box or warning.Need something to fix this error in design, suggestions please.


用户。 php控制器

user.php controller



<?php 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('user_agent');
        $this->load->library('form_validation');
    }

    public function create_user() {
        // field name, error message, validation rules
        $lang = $this->input->post("lang");
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('city', 'City', 'trim|required');
        if ($this->form_validation->run() == FALSE) {
            if ($lang == "en") {
                if ($this->agent->is_mobile()) {
                    $this->load->view('m_english_signup');
                } 
                else 
                {
                    $this->load->view('d_english_signup');
                }
                } //if ($this->agent->is_mobile())
            else 
            {
                if ($this->agent->is_mobile()) {
                    $this->load->view('m_arabic_signup');
                } 
                else 
                {
                    $this->load->view('d_arabic_signup');
                }
            }
        } 
        else 
        {
            $this->load->model('Users_model');
            if ($query = $this->Users_model->create_member()) {
                if ($lang == "en") {
                    if ($this->agent->is_mobile()) {
                        $this->load->view('m_english_thanks');
                    } 
                    else 
                    {
                        $this->load->view('d_english_thanks');
                    }
                } 
                else
                {
                    if ($this->agent->is_mobile()) {
                        $this->load->view('m_arabic_thanks');
                    } 
                    else 
                    {
                        $this->load->view('d_arabic_thanks');
                    }
                }
            }
        }
    }

}


推荐答案

我认为Nilesh有你需要的最好的解决方案。通过使用JavaScript,您可以,如果输入的电子邮件已经存在,您可以生成警报。或者,您也可以使用 Bootstrap 设置 div 的样式

I think Nilesh has the best solution that you need. By using javascript you can, if the email entered already exists, you can generate an alert. Alternatively you can use Bootstrap to style your div(assuming you have one above your email input):

<div class="alert" style="display: none"> 
    <a class="close" data-hide="alert" >×</a>  
    <Strong><?php echo form_error('email'); ?></strong>
</div>
<input name="email" value="<?php echo set_value('email'); ?>" />

然后写一些javascript:

Then write some javascript:

$(document).ready(function(){
    $("[data-hide]").on("submit", function(){
        $("." + $(this).attr("data-hide")).hide();
    });
});

我希望这有助于。

这篇关于CodeIgniter - 唯一值 - 错误显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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