Welcome Guest, Not a member yet? Register   Sign In
Form Validation - Callback to a Model?
#1

Hello,
my User_model have a public method called is_unique_email($email). This method checks if a user has a uniqe mail adress with some status flag checks. This is also the reason why I can't use the standard is_unique validation rule from CodeIgniter.

I'm using a form_validation.php with config array for my validation rule groups. My question ist: How can I call the model method for checking the new users e-mail adress? I searched and tried so many things, but nothing work. My preferred call would be with | pipe  separator.

Like: trim|required|max_length[70]|valid_email|<~ here comes the model callback ~>

Is there any solution for this callback or is there no way and I have to extend the Form_validation system library?

Thanks in advance!
Reply
#2

(This post was last modified: 02-01-2018, 04:31 AM by Zeff.)

(02-01-2018, 12:56 AM)minsk832 Wrote: Hello,
my User_model have a public method called is_unique_email($email). This method checks if a user has a uniqe mail adress with some status flag checks. This is also the reason why I can't use the standard is_unique validation rule from CodeIgniter.

I'm using a form_validation.php with config array for my validation rule groups. My question ist: How can I call the model method for checking the new users e-mail adress? I searched and tried so many things, but nothing work. My preferred call would be with | pipe  separator.

Like: trim|required|max_length[70]|valid_email|<~ here comes the model callback ~>

Is there any solution for this callback or is there no way and I have to extend the Form_validation system library?

Thanks in advance!

Hi Minsk832,

I've you're trying to shift form_validation to the model (I was inspired by Lonnie Ezell's MY_Model), maybe you're struggling with the same issue as I had: check 'use-thing-as-a-rule' and check  proper formatting (see https://forum.codeigniter.com/thread-69930.html).

You should update the validation method in your model to something like this:
PHP Code:
function is_unique_email($email)
{
 
   $this->form_validation->set_message('whatever_you_name_it''This email address is not unique!');
 
   $this->db->where('email',$email);
 
   $query $this->db->get('your_model_table');
 
   if ($query->num_rows() > 0){
 
       return true;
 
   }
 
   else{
 
       return false;
 
   }


Good luck!

Zeff
Reply
#3

Hello Zeff,

at the moment I extend the CI_Form_validation which make it possible to access the is_unique_email method:

PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class 
MY_Form_validation extends CI_Form_validation
{    
    public function 
is_unique_email($value)
    {
        return 
$this->CI->User_model->is_unique_email($value);
    }


form_validation.php:

PHP Code:
<?php
$config 
= array
{
    
'error_prefix' => '',
    
'error_suffix' => '',
    
'create_user' => array
    {
        array
        (
            
'field'     => 'email',
            
'label'     => 'E-Mail',
            
'rules'     => 'trim|required|max_length[70]|valid_email|is_unique_email'
        
),
    },


But I don't unterstand how I can call model method from this rule config array directly.
Reply
#4

You can't.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB