Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 callback validation set error on property of field
    primarykey
    data
    text
    <p>I'm using the callback validator in Symfony 2.0.4 to validate a collection of embedded forms. I'm trying to see whether one of the forms' fields is duplicated in any of the forms and if so to add an error on that field.<br> Here is the code: </p> <pre><code>/** * @Assert\Callback(methods={"isPriceUnique"}) */ class Pricing { protected $defaultPrice; protected $prices; public function isPriceUnique(ExecutionContext $context){ //Get the path to the field that represents the price collection $propertyPath = $context-&gt;getPropertyPath().'.defaultPrice'; $addedPrices = $this-&gt;getPrices(); \array_unshift($addedPrices, $this-&gt;getDefaultPrice()); for($i=0; $i&lt;\count($addedPrices); $i++){ $price=$addedPrices[$i]; $country=$price-&gt;getCountry(); //Check for duplicate pricing options(whose country has been selected already) if($i&lt;\count($addedPrices)-1){ for($j=$i+1; $j&lt;\count($addedPrices); $j++){ if(\strcmp($country, $addedPrices[$j]-&gt;getCountry())==0){ $context-&gt;setPropertyPath($propertyPath.'.'.$j.'.country'); $context-&gt;addViolation('product.prices.unique', array(), null); break; } } } } } </code></pre> <p>The $prices field is an array of Price entities, each having the country property. The country is added to the form type as well.<br> I would like to add the error on the country property only, but it seems that Symfony does not allow me to specify anything more complex than $propertyPath.'.field'.<br> Could anyone tell me which is the syntax for $propertyPath which would allow me to specify a field set on a lower level in the hierarchy?</p> <h2><strong>Update:</strong></h2> <p>It seems that the validation is done properly and after studying the PropertyPath class constructor I am certain that the path that I've used is correct. The issue is that the specified message is not displayed on the country field, as expected.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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