1

Updated:

RJZ:

TdjxQetc is the $activateCode that comes from the DB so when I run /confirm/ I should be getting Sorry you did not have a correct Activation Code as I am not passing in any var ($activateCode) but when I run /confirm/$activateCode I should get Thanks your account is now active you may login! and with the else statement

I am thinking that it should be changed and a new model function developed to check if userActive has been set to 1 and then display another $message so that the link can only be used.


View:

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

Controller:

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

I am unsure why I am not getting the validation messages, I just get my view. The database is updating to 1.

View:

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

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

Controller:

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

Confirm Function:

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

Core Model:

function companyDetails()
    {
        static $details;

        if(!$details)
        {
            $this->db->select('coreCompanyName, coreContactName, coreContactEmail');
            $details = $this->db->get('core')->first_row();
        }
        return $details;
    }
2
  • To be sure have you echo'ed form error in you view..? Commented Mar 5, 2012 at 5:11
  • @Sudhir I have included my view. Commented Mar 6, 2012 at 0:00

2 Answers 2

1

You are kind of making a mountain out of a mole hill here Jess. Let's see what we can do to clean this up:

Controller Method

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

View

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

Please add this code to the top of your UserModel::confirm_user method:

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

I've simplified your controller method to only two cases -- success or error. There is no need to check activate_code because your model is doing that for you.

In addition, I prefer to keep strings that are only used in views where they belong -- in the view.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that, I have tried that but when I try /users/confirm/ I get Your account has been activated. Is there any way that I can set this too, Sorry you have not entered a code?
If you're receiving that message using the above code, your UserModel::confirm_user method is returning true for a blank activation code.
Thanks Jess, so I guess this approach helped or solved your problem?
0

If you haven't already, you do need to call the validation_errors() helper (or one of Codeigniter's other helpers for error generation) in your view file.

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

If you're calling validation_errors() and still not seeing output, it's probably because validation isn't running. Prior to calling the view, you need to run form validation:

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

With the code you provided above, that still won't quite take care of things--you haven't actually set up any rules for validation. You'll want to read up on the validation guide, but the general approach looks like this:

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

You may be wondering what add_rules is actually testing. In Codeigniter, it's generally designed to work on any form data $_POSTed from a form. To use it directly on a model (as it looks like you're trying to do) will take some hacking, and it may be easiest to just use a flag variable and a message string:

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
}

In view:

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

14 Comments

Hey, I have included my view :)
And updated the answer to match! The form_validation library's designed to solve a different problem than you're trying to use it for here--I hope the alternative I've suggested here meets your needs.
Thanks Mate, I have updated my question as I am still not getting a response :)
One thing I noticed quickly is a syntax error here in your updated view: <?php $confirmMessage?>. Did you mean <?php echo $confirmMessage; ?>
Thanks, But that did not solve the issue. I have just done a var_dump() and got the following: bool(false) string(48) "Thanks your account is now active you may login!"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.