Documentation

Table Of Contents

Previous topic

< Class Phalcon\Validation\Validator\StringLength

Next topic

Class Phalcon\Validation\Validator\Url >

This Page

Class Phalcon\Validation\Validator\Uniqueness

extends abstract class Phalcon\Validation\CombinedFieldsValidator

implements Phalcon\Validation\ValidatorInterface

Source on GitHub

Check that a field is unique in the related table

<?php

use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;

$validator->add(
    "username",
    new UniquenessValidator(
        [
            "model"   => new Users(),
            "message" => ":field must be unique",
        ]
    )
);

Different attribute from the field:

<?php

$validator->add(
    "username",
    new UniquenessValidator(
        [
            "model"     => new Users(),
            "attribute" => "nick",
        ]
    )
);

In model:

<?php

$validator->add(
    "username",
    new UniquenessValidator()
);

Combination of fields in model:

<?php

$validator->add(
    [
        "firstName",
        "lastName",
    ],
    new UniquenessValidator()
);

It is possible to convert values before validation. This is useful in situations where values need to be converted to do the database lookup:

<?php

$validator->add(
    "username",
    new UniquenessValidator(
        [
            "convert" => function (array $values) {
                $values["username"] = strtolower($values["username"]);

                return $values;
            }
        ]
    )
);

Methods

public validate (Phalcon\Validation $validation, mixed $field)

Executes the validation

protected isUniqueness (Phalcon\Validation $validation, mixed $field)

...

protected getColumnNameReal (mixed $record, mixed $field)

The column map is used in the case to get real column name

public __construct ([array $options]) inherited from Phalcon\Validation\Validator

Phalcon\Validation\Validator constructor

public isSetOption (mixed $key) inherited from Phalcon\Validation\Validator

Checks if an option has been defined

public hasOption (mixed $key) inherited from Phalcon\Validation\Validator

Checks if an option is defined

public getOption (mixed $key, [mixed $defaultValue]) inherited from Phalcon\Validation\Validator

Returns an option in the validator’s options Returns null if the option hasn’t set

public setOption (mixed $key, mixed $value) inherited from Phalcon\Validation\Validator

Sets an option in the validator

Follow along: