CodeIgniter:显示动态错误

时间:2012-03-05 05:08:31

标签: php validation codeigniter

更新

RJZ:

TdjxQetc是来自数据库的$activateCode所以当我运行/确认/我应该得到抱歉时,你没有正确的激活码,因为我没有传递任何var({ {1}})但是当我运行/确认/ $activateCode我应该感谢您的帐户现已激活您可以登录!并使用else语句

我认为应该更改它并开发一个新的模型函数来检查$activateCode是否已设置为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>

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

查看:

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

控制器:

<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;
        }

2 个答案:

答案 0 :(得分:1)

你有点像在这里的鼹鼠山上建造一座山。让我们看看我们可以做些什么来清理它:

控制器方法

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,因为您的模型正在为您执行此操作。

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

答案 1 :(得分:0)

如果您还没有,则需要在视图文件中调用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中,它通常用于处理来自表单的任何表单数据$_POST。要在模型上直接使用它(因为它看起来像你正在尝试做的那样)将需要一些黑客攻击,并且最简单的方法是使用标志变量和消息字符串:

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; ?>