<p dir="auto">As of Symfony 2.1, each field supports custom constraints in the "constraints" option. Constraints in this option should (probably) prevent the appropriate field in the model from being updated. Updating the model <em>before</em> launching validation was repeatedly criticized in Symfony2. This change could give the best of both worlds.</p>
<p dir="auto">Example:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php

$builder-&gt;add('ip', 'text', array(
    // setIp() will not be called if the regex fails
    'constraints' =&gt; new Regex('~^(\d{3}\.){3}\d{3}/\d{1,3}$~'),
));"><pre class="notranslate"><span class="pl-ent">&lt;?php</span>

<span class="pl-s1"><span class="pl-c1">$</span>builder</span>-&gt;<span class="pl-en">add</span>(<span class="pl-s">'ip'</span>, <span class="pl-s">'text'</span>, <span class="pl-en">array</span>(
    <span class="pl-c">// setIp() will not be called if the regex fails</span>
    <span class="pl-s">'constraints'</span> =&gt; <span class="pl-k">new</span> <span class="pl-v">Regex</span>(<span class="pl-s">'~^(\d{3}\.){3}\d{3}/\d{1,3}$~'</span>),
));</pre></div>
<p dir="auto">I'm not sure yet whether this can be implemented at all. Thoughts?</p>
<p dir="auto">Sometimes when is created a entity and used the UniqueEntityValidator fails in an update of a existing object.</p>
<p dir="auto">The problem is that in the UniqueEntityValidator use a === an as the PHP's manual said:<br>
"when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class" (<a href="http://php.net/manual/en/language.oop5.object-comparison.php" rel="nofollow">http://php.net/manual/en/language.oop5.object-comparison.php</a>), and the objects aren't the same instance of the class.</p>
<p dir="auto">The === should be change by a simple ==.</p>
<p dir="auto">File: vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php<br>
Line: 117</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    if (0 === count($result) || (1 === count($result) &amp;&amp; $entity == ($result instanceof \Iterator ? $result-&gt;current() : current($result)))) {"><pre class="notranslate"><code class="notranslate">    if (0 === count($result) || (1 === count($result) &amp;&amp; $entity == ($result instanceof \Iterator ? $result-&gt;current() : current($result)))) {
</code></pre></div>