Note that there are some explanatory texts on larger screens.

plurals
  1. POPass custom parameters to custom ValidationConstraint in Symfony2
    primarykey
    data
    text
    <p>I'm creating a form in Symfony2. The form only contains one <code>book</code> field which allows the user to choose between a list of <code>Books</code> entities. I need to check whether the selected <code>Book</code> belongs to an <code>Author</code> I have in my controller.</p> <pre class="lang-php prettyprint-override"><code>public class MyFormType extends AbstractType { protected $author; public function __construct(Author $author) { $this-&gt;author = $author; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('book', 'entity', array('class' =&gt; 'AcmeDemoBundle:Book', 'field' =&gt; 'title'); } // ... } </code></pre> <p>I want to check, after submitting the form, that the selected <code>Book</code> is written by the <code>$author</code> in my controller:</p> <pre class="lang-php prettyprint-override"><code>public class MyController { public function doStuffAction() { $author = ...; $form = $this-&gt;createForm(new MyFormType($author)); $form-&gt;bind($this-&gt;getRequest()); // ... } } </code></pre> <p>Unfortunately, I cannot find any way to do that. I tried creating a custom validator constraint as explained in <a href="http://symfony.com/doc/master/cookbook/validation/custom_constraint.html" rel="noreferrer">The Cookbook</a>, but while I can pass the <code>EntityManager</code> as parameter by defining the validator as a service, I cannot pass the <code>$author</code> from the controller to the validator constraint.</p> <pre class="lang-php prettyprint-override"><code>class HasValidAuthorConstraintValidator extends ConstraintValidator { private $entityManager; public function __construct(EntityManager $entityManager) { $this-&gt;entityManager = $entityManager; } public function validate($value, Constraint $constraint) { $book = $this-&gt;entityManager-&gt;getRepository('book')-&gt;findOneById($value); $author = ...; // That's the data I'm missing if(!$book-&gt;belongsTo($author)) { $this-&gt;context-&gt;addViolation(...); } } } </code></pre> <p><a href="https://stackoverflow.com/questions/10514301/custom-validator-constraint-with-arguments-parameters-in-symfony-2">This solution</a> could be exactly the one that I was looking for, but my form is not bound to an Entity and is not meant to be (I'm getting the data from the <code>getData()</code> method).</p> <p>Is there a solution to my problem ? This must be a common case but I really don't know how to solve it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload