We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Uniqueness validator with soft-delete behavior

Hey there,

I am currently implementing a solution that supports soft-deletes using the native behavior yet also supports a Uniqueness validator that does not return false for soft-deleted rows.

Currently, my replacement validator looks like this:

class Uniqueness extends Validator implements ValidatorInterface
{

    /**
     * @param \Phalcon\Mvc\ModelInterface $record
     * @return bool
     */
    public function validate($record)
    {
        $field = $this->getOption('field');
        $value = $record->readAttribute($field);

        /**
         * Don't validate this on UPDATE operations
         *
         * @todo Implement some check for UPDATE, too, as this could lead to duplicates after edits.
         */
        if ($record->getOperationMade() != 2) {
            if ($record->count(
                    [
                        'conditions' => $field . ' = ?1 AND deleted = ' . Model::NOT_DELETED,
                        'bind'       => [1 => $value]
                    ]
                ) >= 1
            ) {
                $this->appendMessage($this->getOption('message'), $field);
                return false;
            }
        }
        return true;
    }
}

However, I do not know how to change my current dirty-hack (check operationMade) for UPDATE to allow soft-deleting existing records that would trigger a "...already exists" otherwise. Is there a way to see if the soft-delete behavior is invoked?

Thanks in advance!

Best, Philipp

Hello Philipp.

Did you manage to solve this?