Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UniqueObject validator - field issue #380

Closed
imonteiro opened this issue Feb 14, 2014 · 2 comments
Closed

UniqueObject validator - field issue #380

imonteiro opened this issue Feb 14, 2014 · 2 comments

Comments

@imonteiro
Copy link

Hi,

I'm using UniqueObject validator but I receive this error:

Expected context to contain id

In my fieldset definition, I have this:

public function getInputFilterSpecification()
{
    return array(
        'identifier' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'DoctrineModule\Validator\UniqueObject',
                    'options' => array(
                        'object_manager' => $this->objectManager,
                        'object_repository' => $this->objectManager->getRepository('Project\Entity\Project'),
                        'fields' => 'identifier',
                        'messages' => array(
                            'objectNotUnique' => 'Sorry, a project with this identifier already exists !',
                        )
                    )
                ),
            ),
        ),
        'name' => array(
            'required' => true
        ),
        'description' => array(
            'required' => true
        ),
    );
}

My table have id primary key and identifier is a unique field (string).

This only works on primary keys? (if I set 'fields' => 'id', I don't receive the error).
What I'm missing?

Thanks in advance.
IM

@Ocramius
Copy link
Member

The problem here is that we actually use the $context parameter passed to isValid (which is wrong, IMO).

Yes, your compared fields should be the same ones reported in the fields option of the validator.

Dupe of #377

@smozgur
Copy link

smozgur commented Sep 24, 2023

I had the same error in 2023.
The solution is to include the "id" field of the entity as the form element in the form class as a hidden field.

$this->add([
    'name' => 'id',
    'type' => Element\Hidden::class
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants