• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MongoDB + Form_Validation Method is_unique()

#1
[eluser]sweetD[/eluser]
So I wanted to use the is_unique rule in my form validations, but since I'm using Alex Bilbie's Mongo Library, I needed to extend From_validation from the core. Check out what I did below, just wanted to make a quick contribution. I want it to check if lowercase also so I need to add that in there. For example right now if you enter "Peter" and is_unique "peter" it will not be the same and peter lowercase will be allowed even though "Peter" exists.

Code:
array(
    'field' => 'picture_tag',
    'label' => 'Picture Tag',
    'rules' => 'trim|required|min_length[3]|max_length[32]|alpha_numeric|is_unique[users/personal_information.picture_tag]'
   )

Code:
<php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

public function __construct()
    {
        parent::__construct();
    }

public function is_unique($str,$field)
{
  list($collection,$field) = explode('/', $field);
  
  $q = $this->CI->mongo_db->limit(1)->where(array($field => $str))->count($collection);
  
  return $q === 0;

    }

}

#2
[eluser]jim-_-[/eluser]
if you just run this function on both the string and what you are checking against that would even the playing field I think.

Code:
strtolower($str);

#3
[eluser]sweetD[/eluser]
Yea, I messed up I should strtolower when entering the data, then strtolower when comparing.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.