Validation rule for a composite unique index (non-primary)
Asked Answered
D

3

7

I am sure I am not the first who has composite unique keys in tables and who wants to validate them. I do not want to invent the bicycle so I ask here first. I have several tables that have 'id' columns as primary keys and two other columns as unique composite keys. It would be nice to have a validation rule to check that the submitted entry is unique and display a validation error if it is not. In Cakephp it could be done by a custom validation rule. I am pretty sure somebody has created such method already.

Ideally it would be a method in app_model.php that could be used by different models.

Displode answered 10/8, 2010 at 1:51 Comment(0)
A
10

I am using that function:

function checkUnique($data, $fields) {
    if (!is_array($fields)) {
            $fields = array($fields);
        }
        foreach($fields as $key) {
            $tmp[$key] = $this->data[$this->name][$key];
        }
    if (isset($this->data[$this->name][$this->primaryKey]) && $this->data[$this->name][$this->primaryKey] > 0) {
            $tmp[$this->primaryKey." !="] = $this->data[$this->name][$this->primaryKey];
        }
    //return false;
        return $this->isUnique($tmp, false); 
    }

basically the usage is:

'field1' => array(
                'checkUnique' => array(
                    'rule' => array('checkUnique', array('field1', 'field2')),
                    'message' => 'This field need to be non-empty and the row need to be unique'
                ),
            ),
'field2' => array(
                'checkUnique' => array(
                    'rule' => array('checkUnique', array('field1', 'field2')),
                    'message' => 'This field need to be non-empty and the row need to be unique'
                ),
            ),

So basically this will show the warning under each of the fields saying that it's not unique.

I am using this a lot and it's working properly.

Akimbo answered 10/8, 2010 at 7:33 Comment(1)
Thanks! That is exactly what I wanted. The only outpoint is that it produces 2 identical queries when 2 fields are validated.Displode
A
0

In the CakePHP/2.x versions released in the last few years, the isUnique rule optionally accepts several columns:

You can validate that a set of fields are unique by providing multiple fields and set $or to false:

public $validate = array(
    'email' => array(
        'rule' => array('isUnique', array('email', 'username'), false),
        'message' => 'This username & email combination has already been used.'
    )
);

I'm not sure of the exact version when the feature was available but there's a bug fixed in core as late as October 2014 filed against 2.3 and 2.4 branches.

Anticline answered 21/3, 2017 at 16:56 Comment(0)
S
-1

You could put it in app model, but my suggestion would just be to add it to the model directly by placing the rule with it's $validate property.

Check out the built in isUnique rule.

Stamper answered 10/8, 2010 at 1:58 Comment(2)
'isUnique' validates only one field for a single column. I need the validation rule for two columns that compose the composite unique key.Displode
I see. Sorry. Then yes, if this is something in multiple models, I would built a custom validation rule and put it in app_model.php.Stamper

© 2022 - 2024 — McMap. All rights reserved.