⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

[SOLVED] setRule - How to locate duplicate values ?



wildfandango
  • profile picture
  • Member

Posted 07 October 2017 - 17:27 PM

Hi All...
I have a customer table where I must control that there are not two customers with the same VAT.
 
My first idea was to perform a SELECT searching for the current VAT, the SELECT should search the entire table, BUT not in the current record. So if I am editing a client that already exists, I will always find their VAT in the database.
 
I have tried to use VALITRON as follows
            \Valitron\Validator::addRule('chkVAT', function($field, $value, array $params, array $fields) {        
             if ($value) {
                $query = $this->db->query("SELECT * FROM customers where (vat = '".$value."') AND (id <> ".$params[0].")");
                if (empty($query->result())) {
                    return true;
                } else {
                    return false;
                }


            } else {
                // not validate empty docs
                return true;
            }


            return false;
        }, ', another customer has the same VAT.');

and the setRULE call...

        $crud->setRule('vat', 'chkVAT',[ $crud->$fields->id]);

How can I get the ACTIVERECORD ID inside VALITRON or how can I pass it as a parameter???

 

thx in advance!...

 

 

 

 


darkstalker
  • profile picture
  • Member

Posted 07 October 2017 - 22:22 PM

Maybe i don’t understood what you are trying to do, but you can’t just use the uniqueField function for the vat field?

wildfandango
  • profile picture
  • Member

Posted 09 October 2017 - 11:34 AM

That's!
 
Thank you, I did not know that API function, it's just what I was looking for!
 
In my case it would to put (in place all the previous code):
 
$crud->uniqueFields(['vat']);
 
It simplifies the detection of duplicates, I have to try it, but I'm sure
 
Thank you

darkstalker
  • profile picture
  • Member

Posted 09 October 2017 - 18:23 PM

Yes yes, is that simple :)
Happy that you resolved