Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 6f21cb07ef
Fetching contributors…

Cannot retrieve contributors at this time

80 lines (55 sloc) 1.67 kB

The UniqueObjectValidator

In a form, if you want to validate the uniqueness of a field in a table you have to use the UniqueObjectValidator.

You may use as many validators of this type as you want.

YAML

You can specify this using the validation.yml file, like this:

Acme\DemoBundle\Model\User:
    constraints:
        - Propel\PropelBundle\Validator\Constraints\UniqueObject: username

If you want to validate the uniqueness of more than just one field:

Acme\DemoBundle\Model\User:
    constraints:
        - Propel\PropelBundle\Validator\Constraints\UniqueObject: [username, login]

PHP

You can also specify this using php. Fields can be specified as a string if there is only one field

use Propel\PropelBundle\Validator\Constraint\UniqueObject;

...

    /**
     * Load the Validation Constraints
     *
     * @param ClassMetadata $metadata
     */
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addConstraint(
            new UniqueObject(
                array(
                    'fields' => 'username',
                    'message' => 'We already have a user with {{ fields }}'
                )
            )
        );
    }

If there is more than one field you must use an array


...

        $metadata->addConstraint(
            new UniqueObject(
                array(
                    'fields' => array('username', 'login')
                    'message' => 'We already have a user with {{ fields }}'
                )
            )
        );

...

Back to index

Jump to Line
Something went wrong with that request. Please try again.