I'm using a base model for all models, where I'm using a logical delete function overriding default delete function, and at the same time I'm overriding find() method also, to only show records that are logically not deleted. Now I have a rule in one of my models, to check if the combination of the two values are already taken or not. It seems this uses also the find() method. If there is a combination already, but logically deleted, I won't get the error message that this was already taken in the past. Because if the user would once more create the record with the same values, I would like rather to unlock it (or delete the logical deleted state of it). Is there a workaround for this? Maybe with scenarios? I have no clue how can I customize find() method further to search not only for logically not deleted records, only when validating for insert. Otherwise I would have to delete
...find()->where(['softDeleteTimestamp' => NULL])
from my base model and add it to all search models, what would be not really nice. Thanks.
I think you can use the standard UniqueValidator in this way:
<?php
// a1 and a2 need to be unique together, only a1 will receive error message
['a1', 'unique', 'targetAttribute' => ['a1', 'a2']]
If a1 is your unique field and a2 is an is_deleted flag, and before validation you put is_deleted flag to 0, the unique validator search for your unique field and is_deleted=0, so you can have the same unique value in the database if is soft deleted, and you can correctly have only one record not soft deleted.