Cake\ORM\Table::validateUnique PHP Method

validateUnique() public method

This is meant to be used with the validation API and not to be called directly. ### Example: $validator->add('email', [ 'unique' => ['rule' => 'validateUnique', 'provider' => 'table'] ]) Unique validation can be scoped to the value of another column: $validator->add('email', [ 'unique' => [ 'rule' => ['validateUnique', ['scope' => 'site_id']], 'provider' => 'table' ] ]); In the above example, the email uniqueness will be scoped to only rows having the same site_id. Scoping will only be used if the scoping field is present in the data to be validated.
public validateUnique ( mixed $value, array $options, array $context = null ) : boolean
$value mixed The value of column to be checked for uniqueness
$options array The options array, optionally containing the 'scope' key. May also be the validation context if there are no options.
$context array Either the validation context or null.
return boolean true if the value is unique
    public function validateUnique($value, array $options, array $context = null)
    {
        if ($context === null) {
            $context = $options;
        }
        $entity = new Entity($context['data'], ['useSetters' => false, 'markNew' => $context['newRecord'], 'source' => $this->registryAlias()]);
        $fields = array_merge([$context['field']], isset($options['scope']) ? (array) $options['scope'] : []);
        $values = $entity->extract($fields);
        foreach ($values as $field) {
            if ($field !== null && !is_scalar($field)) {
                return false;
            }
        }
        $rule = new IsUnique($fields, $options);
        return $rule($entity, ['repository' => $this]);
    }