Welcome Guest, Not a member yet? Register   Sign In
Form_validation help!
#1

[eluser]Rogier[/eluser]
I just can't figure out what i'm doing wrong... I'm new to Codeigniter so maybe it's a simple mistake but please help me.. For somekind of reason the callback won't work.....

Thanks....

class Site extends Controller {

function __construct() {

parent::__construct();

$this->load->library(array('session', 'form_validation'));

$this->load->helper(array('url', 'form'));

$this->load->database();

}

function index() {

redirect('site/subscribe');

}

function subscribe() {

$this->load->model('mdl_subscribe');

if ($this->mdl_subscribe->validate()) {

if ($this->mdl_subscribe->create('camp_subscribers', 'sub_name', 'sub_email', $this->input->post('sub_name'), $this->input->post('sub_email'))) {

echo 'email send';

}
}

$this->load->view('site');

}




---- the model -----

class Mdl_subscribe extends MY_Model {

public function __construct() {

parent::__construct();

}

public function validate() {

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

$this->form_validation->set_rules('sub_name', 'Subscriber name', 'trim');

$this->form_validation->set_rules('sub_email', 'Subscriber email', 'trim|required|valid_email|callback_check_subscribers');

return parent::validate();

}

public function create($table, $sub_name_field, $sub_email_field, $sub_name, $sub_email) {

$db_array = array(
$sub_name_field => $sub_name,
$sub_email_field => $sub_email,
'sub_active' => '0',
'sub_hash' => md5($sub_email)
);

$this->db->insert($table, $db_array);

return TRUE;

}

public function check_subscribers($mail) {

return FALSE;

//this i just did for the tryout later on i want to check if emailadress already exists in database

}

}
#2

[eluser]mikeymayhem[/eluser]
Hey,

Welcome to CI, please try and use the coding blocks for the code when ya post...makes it much easier to read! Big Grin

Does the validation work on any of the other elements of the form? I mean you are returning FALSE in your callback but your not setting any error messages when you return back to the validation.
#3

[eluser]InsiteFX[/eluser]
The Validation will not work because you need to read the:

CodeIgniter User Guide - Form Validation

Code:
if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myform');
}
else
{
    $this->load->view('formsuccess');
}

InsiteFX
#4

[eluser]Rogier[/eluser]
alright sorry for that.... The form validation works it gives a error message when it is not a valid email and if it is blank so... And i didn't set a message cause thats not required is it??
It should just return a false to the parent.. But instead it returns TRUE...
#5

[eluser]mikeymayhem[/eluser]
Hmmm To be honest i dont normally do my validation in my models so i might not be any help to you. CI likes its validation to be done in the controller. i found this post that might be of use to you
http://ellislab.com/forums/viewthread/123780/

Is there any reason you have to do your validation in you model?? i know it seems like a logical place especially when using callbacks that interact with the db?
#6

[eluser]Rogier[/eluser]
I even tried this:

Code:
public function validate() {
        
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('sub_name', 'Subscriber name', 'trim');

        $this->form_validation->set_rules('sub_email', 'Subscriber email', 'trim|required|valid_email|callback_check_subscribers');
        
        if ($this->form_validation->run() == FALSE) {
            
            return FALSE;
                        
        } else {

            return TRUE;

        }
        
    }

public function check_subscribers($mail) {
      
        $this->form_validation->set_message('check_subscribers', 'it can not be this email address');
        return FALSE;
        
    }

And still... It returns a true when email is valid.. It should return a false because i set the callback to false...
#7

[eluser]mikeymayhem[/eluser]
Just try this out and let me know what happens, remember to enter 'test' in the form input your using the callback on and make sure you have all the correct info for your inputs so the name="" of the input using the callback is defo name="sub_email"


Code:
class Site extends Controller
{

  function Site()
  {
      parent::Controller();
  }

  function index() {

      redirect(‘site/subscribe’);

  }

  public function subscribe() {


      //load the required libraries and helpers (if don't Autoload them already loaded them)
      $this->load->helper(array('form', 'url'));
      $this->load->library('form_validation');

      //set our validation rules
      $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
      $this->form_validation->set_rules('sub_name', 'Subscriber name', 'trim');
      $this->form_validation->set_rules('sub_email', 'Subscriber email', 'trim|required|valid_email|callback_check_subscribers');

      if($this->form_validation->run() == FALSE)
      {
          //validation failed load the view to display the errors
          $this->load->view(‘site’);
      }
      else
      {
          //do stuff here...
          $this->load->model('some_model');  
      }
  }
  public function check_subscribers($mail)
  {
    if($mail == 'test')
    {
        $this->form_validation->set_message('check_subscribers', 'it can not be this email address');
        die('FALSE');
    }
    else
    {
        die('TRUE');
    }

  }
#8

[eluser]mikeymayhem[/eluser]
Also why are you trying to do your validation in a sepearte function??
#9

[eluser]Rogier[/eluser]
@mikeymayhem

thanks for your reply...

this also does not work.. It always returns a true... it looks like the callback function is never called....?? I use modules for this project don't know if this has anything to do with it...

Code:
class MY_Form_validation extends CI_Form_validation {

    function run($module = '', $group = '') {
        (is_object($module)) AND $this->CI =& $module;
        return parent::run($group);
    }

}
#10

[eluser]mikeymayhem[/eluser]
http://www.mahbubblog.com/php/form-valid...deigniter/ this might be of some use. seems like you need to make some adjustments to the form_validation library when using modular extentions in CI hope that is what your after!!




Theme © iAndrew 2016 - Forum software by © MyBB