【问题标题】:CodeIgniter - Unique Value - Error DisplayCodeIgniter - 唯一值 - 错误显示
【发布时间】:2014-01-16 07:29:18
【问题描述】:

我制作了一个简单的表单,一个简报订阅,它有两个输入:emailcity。我在表单验证中使用is_unique,但此错误显示为文本。我需要修改此文本显示在警告框或警告中。需要一些东西来解决设计中的这个错误,请提出建议。

user.php 控制器

<?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');
                    }
                }
            }
        }
    }

}

【问题讨论】:

  • 您在视图上输出错误。您如何处理视图上的验证错误?
  • 我在视图中没有添加任何内容,我只在控制器中使用这个 is_unique,并在上面提到的视图页面上显示错误。我设置的视图请指导。
  • 我认为你应该使用 ajax 调用来检查电子邮件是否已经存在
  • 我在表单中使用 is_unique,它工作正常,但我需要将此错误显示为警报/警告,您可以查看链接 link
  • 好的,这可以通过 javascript 完成,将您的错误存储在控制器中的变量中,例如 $errors = validation_errors();,您可以从 $erros 数组中提取与电子邮件相关的错误并将其存储在一个 $email_error 并在您的视图文件中写入 if(!empty($_POST) && $email_error!=''){?>

标签: php codeigniter


【解决方案1】:

我认为 Nilesh 拥有您需要的最佳解决方案。通过使用 javascript 您可以,如果输入的电子邮件已经存在,您可以生成警报。或者,您可以使用Bootstrap 设置div 的样式(假设您的电子邮件输入上方有一个):

<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:

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

我希望这会有所帮助。

【讨论】:

  • 这个方案可以首选
【解决方案2】:

您需要设置自己的错误信息吗?

试试 CodeIgnitor 用户指南http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#settingerrors

【讨论】:

  • ` $this->form_validation->set_message('is_unique[users.email]','电子邮件地址已经在我们的数据库中选择另一个电子邮件ID。'); $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]'); $this->form_validation->set_rules('city', 'City', 'trim|required');` 我已经用过了,但它不起作用!
猜你喜欢
  • 2015-05-30
  • 2014-05-25
  • 2015-04-20
  • 2016-09-22
  • 1970-01-01
  • 2015-07-28
  • 2016-04-06
  • 1970-01-01
相关资源
最近更新 更多