Yii2 Validate that a table record entry does not exist in another table

Viewed 392

I am developing a system in yii2 that validates that a national_ID entry does not belong to a dead person. So I have a table registration1 and deathregister table. I wanted that when I make an entry in the registration1 it should validate that the Id number entered DOES NOT EXIST in table deathregister.

Currently I have this code which is only validating that the record exists, the question I wanted help is to customize it to check if record does NOT exist.

 ['identitynumber', 'exist', 'targetClass' => Deathregister::className(), 'targetAttribute' => ['nationalidnum' => 'idnum'],'message'=>'The ID number Supplied Belongs to the Dead'],
       
2 Answers

As Bizley suggest, you should do it manually. I think what you need is something like this. You can run a custom validator which will add something to errors or not (based on empty errors array it considers validity)

public function rules()
{
    return  [
        ['identitynumber', 'notExistsValidator'],
    ];
}


public function notExistsValidator()
{
    if(!Deathregister::findOne(['nationalidnum' => $this->identitynumber])->exists())
     {
        $this->addError('nationalidnum', 'The ID number Supplied Belongs to the Dead');
    }
}

As 'exist' is used for availability, the 'unique' validator checks to see if the input value in a table column is unique.
As before targetClass, targetAttribute, filter is valid for unique.
Note: It only works with Active Record model attributes

But you can also use the following methods:
Inline Validators:

return [
    ['identitynumber', 'unique', 
      'targetClass' => Deathregister::className(), 
      'targetAttribute' => ['nationalidnum' => 'idnum'],
      'message'=>'The ID number Supplied Belongs to the Dead'
      /*Also, if validation of one attribute depends on the value of another attribute */
      // 'when' => function ($model) {
      //         if (condition) {
      //             return $this->addError('identitynumber', Yii::t('user', 'The ID number Supplied Belongs to the Dead')); 
      //          }
      //         return false;
      //       }
    ],
];

Example2:

return [
    // validatorMethod()
    ['identitynumber', 'validatorMethod'],
    // or
    ['identitynumber', function ($attribute, $params) {
        if(!Deathregister::findOne(['nationalidnum' => $this->$attribute])->exists()) {
            $this->addError($attribute, 'The ID number Supplied Belongs to the Dead.');
        }
    }],
];


// validatorMethod()
public function ValidatorMethod($attribute, $params)
{
    if(!Deathregister::findOne(['nationalidnum' => $this->$attribute])->exists()) {
        $this->addError($attribute, 'The ID number Supplied Belongs to the Dead');
    }
}

/** Or */
// public function validatorMethod()
// {
      // if(!Deathregister::findOne(['nationalidnum' => $this->identitynumber])->exists())
            # code ...
//     }
// }

You can also extend the class (Standalone Validators)
It is better to refer to this page and this page.
Link2

Note:If you want to make sure a rule is always applied, you may configure the skipOnEmpty and/or skipOnError properties to be false in the rule declarations.

Related