Welcome Guest, Not a member yet? Register   Sign In
New is_unique form validation method NOT WORKING! [SOLVED]
#1

[eluser]Steve Goodwin[/eluser]
So i've been playing around with this most of the afternoon, i've read the documentation and tried multiple ways of calling this in the code, but still no joy.

I want to check weather the users email is unique, the table is called "member_tbl" and the field is called "email".

My controller code is as below:

Code:
public function register()
{
    $this->load->helper('form');
    $this->load->library('form_validation');
  
    if($this->input->post())
    {

        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[member_tbl.email]');

        if ($this->form_validation->run() == FALSE)
        {
            $data['content'] = "<strong>Form produced Errors</strong>";
        }
        else
        {
            $data['content'] = "Form was processed successfully!";
        }
    }
    else
    {
        $data['content'] = "Register";
    }
    
    $data['title'] = "Member Registration";
    
    $this->load->view('member/register', $data);
}

My View code is below:

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

&lt;form name="member-registration" method="post" action="/member/register/"&gt;

    <div class="grid_2">
                                    
        <label for="email">Email: <span class="required">*</span></label>
                            
    </div>
                        
    <div class="grid_2">
                            
        &lt;input type="text" name="email" id="email" value="&lt;?php echo set_value('email'); ?&gt;"&gt;
                            
    </div>

    <div class="grid_4">
                        
        &lt;?php echo form_error('email'); ?&gt;
                            
    </div>

        <div class="clear"></div>
                        
    <div class="grid_2">
                        
        &lt;input type="submit" name="button" id="button" value="Submit"&gt;
                        
    </div>

&lt;/form&gt;

Is there anything I'm doing wrong or is CI 2.1.0 to blame?
#2

[eluser]spiderking[/eluser]
Hello !
This was a great help. But i just wanna add something, if you are checking to see if a email/ or a similar user name has already been registered, then you might also add a line like this :
Code:
$lang['is_unique']      = "This email/username has aleady been registered.";

in their "form_validation_lang.php" located under system/language/english/. This will add/ display the appropriate error message when a user attempt to register twice.


Thanks

#3

[eluser]jmc_1014[/eluser]
you should extends your library and not editing system folder

create a file MY_Form_validation.php in application\libraries

&lt;?php

class MY_Form_validation extends CI_Form_validation{


function is_unique($str, $field)
{
list($table, $field) = explode('.', $field);

$this->CI->form_validation->set_message('is_unique','The %s is not available');

if (isset($this->CI->db))
{
$query = $this->CI->db->where($field, $str)->get($table);
return $query->num_rows() === 0;
}

return FALSE;
}

}

try it it works for me
#4

[eluser]jmc_1014[/eluser]
Quote:[quote author="jmc_1014" date="1332010710"]you should extends your library and not editing system folder

create a file MY_Form_validation.php in application\libraries

&lt;?php

class MY_Form_validation extends CI_Form_validation{


function is_unique($str, $field)
{
list($table, $field) = explode('.', $field);

$this->CI->form_validation->set_message('is_unique','The %s is not available');

if (isset($this->CI->db))
{
$query = $this->CI->db->where($field, $str)->get($table);
return $query->num_rows() === 0;
}

return FALSE;
}

}

try it it works for me
[/quote]
Quote:
Quote:




Theme © iAndrew 2016 - Forum software by © MyBB