【问题标题】:CodeIgniter: Displaying Dynamic ErrorsCodeIgniter:显示动态错误
【发布时间】:2012-03-05 05:08:31
【问题描述】:

更新:

RJZ:

TdjxQetc 是来自数据库的$activateCode,所以当我运行 /confirm/ 时,我应该会感到抱歉,您没有正确的激活码,因为我没有传入任何 var ($activateCode) 但是当我运行 /confirm/$activateCode 时,我应该得到谢谢,您的帐户现在处于活动状态,您可以登录!并使用 else 语句

我在想应该改一下,开发一个新的模型函数来检查userActive是否已经设置为1,然后再显示另一个$message,这样就只能使用链接了。


查看:

<div class = "messages">
    <?php if($confirmMessage != ''): ?>
        <?php if($confirmError): ?>
            <p class="error">
                <?php echo $confirmMessage; ?>
                </p>
                <?php else: ?>
                    <p class="message">
                        <?php $confirmMessage?>
                        </p>
                        <?php endif; ?>
                        <?php endif; ?>
    </div>

控制器:

function confirm(){

        $activateCode = $this->uri->segment(3);
        $error = FALSE;
        $message = '';

        if($activateCode == '')
        {
            $error = TRUE;
            $message = 'Sorry you did not have a correct Activation Code.';
        }
            $userConfirmed = $this->users_model->confirm_user($activateCode);

            if($userConfirmed){
                $message = 'Thanks your account is now active you may login!';
            }else{
                $error = TRUE;
                $message = 'I am sorry we do not have any details with that Activation Code';
            }
            $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
            $data['pageTitle'] = "User Confirm";
            $data['confirmError'] = $error;
            $data['confirmMessage'] = $message;
            $this->load->view('frontend/assets/header', $data);
            $this->load->view('frontend/user_confirm', $data);
            $this->load->view('frontend/assets/footer');
    }

我不确定为什么我没有收到验证消息,我只是了解我的观点。数据库正在更新到 1。

查看:

<h1><?php echo $companyName; echo nbs(1);?> - <?php echo $pageTitle; ?></h1>

    <p>Error: <?php echo validation_errors();?></p>

控制器:

function confirm(){

        $activateCode = $this->uri->segment(3);

        if($activateCode == '')
        {
            $this->form_validation->set_message('userConfirmError', 'Sorry you did not have a correct Activation Code.');
        }
            $userConfirmed = $this->users_model->confirm_user($activateCode);

            if($userConfirmed){
                $this->form_validation->set_message('userConfirmed', 'Thanks your account is now active you may login!');
            }else{
                $this->form_validation->set_message('userRecord', 'I am sorry we do not have any details with that Activation Code');
            }
            $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
            $data['pageTitle'] = "User Confirm";
            $this->load->view('frontend/assets/header', $data);
            $this->load->view('frontend/user_confirm', $data);
            $this->load->view('frontend/assets/footer');
    }

确认功能:

function confirm_user($activateCode)
    {
     //Selects the userID where the given URI activateCode = ?

        $this->db->select('userID');
        $this->db->from('users');
        $this->db->where('userActiveCode', $activateCode);

        $result = $this->db->get();

        if($result->num_rows == 1)  // If the above result is = 1 then update the userActive row else it will fail
        {
            $this->db->set('userActive', 1);
            $this->db->where('userActiveCode', $activateCode);

            return TRUE;
        }else{
            return FALSE;
        }

核心模型:

function companyDetails()
    {
        static $details;

        if(!$details)
        {
            $this->db->select('coreCompanyName, coreContactName, coreContactEmail');
            $details = $this->db->get('core')->first_row();
        }
        return $details;
    }

【问题讨论】:

  • 确定您是否在视图中回显了表单错误..?
  • @Sudhir 我已经包含了我的观点。

标签: php validation codeigniter


【解决方案1】:

Jess,你有点像在鼹鼠山上造山。让我们看看我们可以做些什么来清理它:

控制器方法

function confirm()
{
    $activate_code = $this->uri->segment(3);

    if(!$this->users_model->confirm_user($activate_code))
        $error = true;
    else
        $error = false;

    $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
    $data['pageTitle'] = "User Confirm";
    $data['confirmError'] = $error;
    $this->load->view('frontend/assets/header', $data);
    $this->load->view('frontend/user_confirm', $data);
    $this->load->view('frontend/assets/footer');
}

查看

<div class = "messages">
    <?php if($confirmError): ?>
        <p class="error">
            Your activation code is invalid.
        </p>
    <?php else: ?>
         <p class="message">
            Your account has been activated.
         </p>
    <?php endif; ?>
</div>

请将此代码添加到您的 UserModel::confirm_user 方法的顶部:

if($activateCode == '')
   return false;

我已将您的控制器方法简化为只有两种情况——成功或错误。无需检查 activate_code,因为您的模型正在为您执行此操作。

此外,我更喜欢将仅在它们所属的视图中使用的字符串保留在视图中。

【讨论】:

  • 谢谢您,我已经尝试过了,但是当我尝试 /users/confirm/ 时,我得到您的帐户已被激活。有什么办法我也可以设置这个吗,抱歉你没有输入代码?
  • 如果您使用上述代码收到该消息,则您的 UserModel::confirm_user 方法将返回 true 以获得空白激活码。
  • 谢谢 Jess,所以我想这种方法帮助或解决了您的问题?
【解决方案2】:

如果您还没有,您确实需要在视图文件中调用 validation_errors() 帮助程序(或 Codeigniter 的其他帮助程序之一以生成错误)。

// in view file
<form>
  <?php echo validation_errors(); ?>
  <!-- rest of form... -->
</form>

如果您调用 validation_errors() 仍然没有看到输出,这可能是因为验证没有运行。在调用视图之前,您需要运行表单验证:

// in controller action function
$this->form_validation->run();

使用您在上面提供的代码,仍然无法解决问题——您实际上还没有设置任何验证规则。您需要阅读validation guide,但一般方法如下所示:

// in controller action function
$this->form_validation->add_rules('my_field', 'My Field Name', 'required');
$this->form_validation->run();

您可能想知道add_rules 实际测试的是什么。在 Codeigniter 中,它通常设计用于处理来自表单的任何表单数据$_POSTed。直接在模型上使用它(就像你正在尝试做的那样)需要一些黑客攻击,并且使用标志变量和消息字符串可能是最简单的:

function confirm($activateCode = '') {

  $error = false;
  $message = '';

  if($activateCode == '')
  {
    $error = true;
    $message = 'Sorry you did not have a correct Activation Code.';
  }

  $userConfirmed = $this->users_model->confirm_user($activateCode);

  if($userConfirmed)
  {
    $message = 'Thanks your account is now active you may login!';
  }
  else
  {
    $error = true;
    $message = 'I am sorry we do not have any details with that Activation Code';
  }

  $data['error'] = $error;
  $data['message'] = $message
}

在视图中:

<?php if ($message != ''): ?>
<?php if ($error): ?>
  ERROR: <?php echo $message; ?>
<?php else: ?>
  SUCCESS: <?php echo $message; ?>
<?php endif; ?>
<?php endif; ?>

【讨论】:

  • 嘿,我已经包含了我的观点 :)
  • 并更新了答案以匹配! form_validation 库旨在解决与您在这里尝试使用它不同的问题——我希望我在这里建议的替代方案能够满足您的需求。
  • 谢谢伙计,我已经更新了我的问题,因为我仍然没有得到回复:)
  • 我很快注意到的一件事是更新视图中出现语法错误:。你的意思是
  • 谢谢,但这并没有解决问题。我刚刚做了一个 var_dump() 并得到了以下信息: bool(false) string(48) "谢谢你的帐户现在是活动的,你可以登录了!"
猜你喜欢
  • 2014-05-25
  • 1970-01-01
  • 2018-06-24
  • 2016-09-22
  • 2013-12-22
  • 2015-07-28
  • 2016-04-06
  • 1970-01-01
相关资源
最近更新 更多