Welcome Guest, Not a member yet? Register   Sign In
Need help with code ;)
#1

[eluser]ThatsJustMe[/eluser]
Hello,

So basically, I'm validating a registration form, and if there are any errors, I'm returning them back to controller, so they are displayed in View.

Here are my code -

Model -

Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Registration_validate extends CI_Model {

  function __construct() {
    parent::__construct();
  }
  
  function checkData($name, $email, $password, $passwordRepeat, $username, $code, $message) {
    global $error;
    if($name == '' || $email == '' || $password == '' || $passwordRepeat == '' || $username == '' || $code == '') {
      $error.= "All fields must be filled!";  
    }
    else {
      if(strlen($name) <= 2) {
        $error.= "Tavs ievadītais vārds ir par īsu! <br />";  
      }
    }
    if($error) {
      return $error;
      header("Location: ../registration");
    }
  }

}
?&gt;

Controller -


&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Code:
class Registration extends CI_Controller {

public function index()
{
  global $error;
  echo $error;
   $data['error'] = $error;
    $this->load->helper('url');
    $this->load->view('includes/header');
  $this->load->view('registration', $data);
  $this->load->view('includes/footer');
  }
  
  public function registrate() {
    if(isset($_POST['registrate'])) {
    $this->load->model('registration_validate');
    $this->registration_validate->checkData($_POST['name'], $_POST['email'], $_POST['password'], $_POST['password-repeat'], $_POST['uname'], $_POST['code'], $_POST['message']);  
    }
    else {
      header("Location: ../registration");
    }
  }

} ?&gt;

View -

Code:
<p>&lt;?php echo $error; ?&gt;</p>

Hope you will be able to help Wink!
#2

[eluser]Matalina[/eluser]
why aren't you using the validation class in CI?
#3

[eluser]pickupman[/eluser]
You would want to use the [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html"]Form Validation[/url] library in your controller. If the form submission passes form validation, then process data to your model.
#4

[eluser]CodeIgniteMe[/eluser]
And avoid header() redirection if you want to preserve your validation messages. If you every need to use header(), then use redirect() from the URL Helper
#5

[eluser]ThatsJustMe[/eluser]
Hey!

Thank's to all for replying! I'm using now Form validation and redirect(), works great Wink! Last question, how can I check if for example entered activation code in form is inside a database with the form_validation? I saw that you can check if username is unique, but is there any chance to check if the activation code is in database, and if it is then show no errors, otherwise show errors?

Best regards,

ThatsJustMe
#6

[eluser]pickupman[/eluser]
As the guide point out for form validation, the input gets passed as the first argument of the function/method. Example:
Code:
$this->form_validation->set_rules('activation_code', 'activation code', 'required|trim|callback_check_validation');

//In same controller
function check_validation($validation)
{
  return $this->validation_model->get_code($validation);
}

//Validation model
function get_code($code)
{
  $query = $this->db->get_where('user', array('activation_code' => $code));
  return ($query->num_rows() > 0) ? TRUE : FALSE;
}
#7

[eluser]ThatsJustMe[/eluser]
Thanks Wink!
#8

[eluser]ThatsJustMe[/eluser]
Fast question - it gives me error - Call to a member function get_where() on a non-object in.... do I need to include any kind of DB libraries? I have specified db settings in config/database.php file.

EDIT: Aha - found - $this->load->database(); .
#9

[eluser]CodeIgniteMe[/eluser]
Good to know




Theme © iAndrew 2016 - Forum software by © MyBB