Skip to content

[Validator] Explained how to use array constant in annotation #10810

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

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ constraint.
}
}

If the callback is stored in a different class and is static, for example ``AppBundle\Entity\Genre``,
If the callback is defined in a different class and is static, for example ``AppBundle\Entity\Genre``,
you can pass the class name and the method as an array.

.. configuration-block::
Expand Down Expand Up @@ -279,6 +279,35 @@ you can pass the class name and the method as an array.
}
}

Supplying the Choices from an Array Constant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array constant in lower case is better i think, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you're right

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i've wrote like this, to be consistent with the title above which is Supplying the Choices with a Callback Function

--------------------------------------------

You can also directly provide a constant containing an array to the ``choices`` option in the annotation::

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
const GENRES = ['action', 'comedy'];

/**
* @Assert\Choice(choices=Author::GENRES)
*/
protected $genre;
}

.. warning::

The constant in the option is used without quotes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about both inside note instead of warning? warning can be misunderstood

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean in the same note or two different notes ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean both inside the same note ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same i think


.. note::

If the constant is defined in a different class, you can pass the fully qualified class name.


Available Options
-----------------

Expand Down