Phalcon\Validation\Validator\Db\Uniqueness::validate PHP Method

validate() public method

Executes the uniqueness validation
public validate ( Phalcon\Validation $validator, string $attribute ) : boolean
$validator Phalcon\Validation
$attribute string
return boolean
    public function validate(Validation $validator, $attribute)
    {
        $table = $this->db->escapeIdentifier($this->getOption('table'));
        $column = $this->db->escapeIdentifier($this->getOption('column'));
        if ($this->hasOption('exclude')) {
            $exclude = $this->getOption('exclude');
            $result = $this->db->fetchOne(sprintf('SELECT COUNT(*) AS count FROM %s WHERE %s = ? AND %s != ?', $table, $column, $this->db->escapeIdentifier($exclude['column'])), Db::FETCH_ASSOC, [$validator->getValue($attribute), $exclude['value']]);
        } else {
            $result = $this->db->fetchOne(sprintf('SELECT COUNT(*) AS count FROM %s WHERE %s = ?', $table, $column), Db::FETCH_ASSOC, [$validator->getValue($attribute)]);
        }
        if ($result['count']) {
            $message = $this->getOption('message', 'Already taken. Choose another!');
            $validator->appendMessage(new Message($message, $attribute, 'Uniqueness'));
            return false;
        }
        return true;
    }