<p dir="auto">The example given for setting a life cycle event does not use a setter method at:</p>
<p dir="auto"><a href="http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks" rel="nofollow">http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks</a></p>
<p dir="auto">The current example is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/**
* @ORM\PrePersist
 */
public function setCreatedAtValue()
{
    $this-&gt;createdAt = new \DateTime();
}"><pre class="notranslate"><code class="notranslate">/**
* @ORM\PrePersist
 */
public function setCreatedAtValue()
{
    $this-&gt;createdAt = new \DateTime();
}
</code></pre></div>
<p dir="auto">I assume it should be something like the following (assuming that there is a database column "createdAt" that has had getters and setters generated for it:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/**
* @ORM\PrePersist
 */
public function setCreatedAtValue()
{
    $this-&gt;setCreatedAt(new \DateTime());
}"><pre class="notranslate"><code class="notranslate">/**
* @ORM\PrePersist
 */
public function setCreatedAtValue()
{
    $this-&gt;setCreatedAt(new \DateTime());
}
</code></pre></div>
<p dir="auto">Having a "createdAt" DateTime field that does not get updated after the initial submission is a common need, and it would be nice if it were more thoroughly explained (including how to exclude it from display in a Twig template). FWIW, I still can't get this to work right, so my suggestion may be misguided or incomplete.</p>
<p dir="auto"><strong>Symfony version(s) affected</strong>: x.y.z<br>
4.1.1</p>
<p dir="auto"><strong>Description</strong><br>
By using a ChoiceType as a container for a select, and adding options dynamically in the form (e.g. from ajax request) means that the values cannot be passed in the FormType class, which inturn means validation always failed. It's impossible to modify allowed choices in a form event</p>
<p dir="auto"><strong>How to reproduce</strong></p>
<ul dir="auto">
<li>Create a form which uses a <code class="notranslate">ChoiceType</code> input, pass in an empty array for the choices, and dynamically populate the input in javascript: untested jquery code</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$(function(){
    $('#foo').append(new Option('new option', 'newoption'));
});"><pre class="notranslate"><code class="notranslate">$(function(){
    $('#foo').append(new Option('new option', 'newoption'));
});
</code></pre></div>
<ul dir="auto">
<li>submit form and see failed validation for input</li>
</ul>
<p dir="auto"><strong>Possible Solution</strong></p>
<p dir="auto">Being able to modify the parameters that are used in the creation of the input would be great so I could fetch the permitted values from the db based upon other input and then add them to the choices.</p>
<p dir="auto">At the moment, I'm having to do something like this in a FormEvent::SUBMIT event listener:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="            $input = $form-&gt;get('itemCondition');
            $value = $input-&gt;getViewData();
            $form-&gt;remove('itemCondition');
            $obj-&gt;setItemCondition($value);"><pre class="notranslate"><code class="notranslate">            $input = $form-&gt;get('itemCondition');
            $value = $input-&gt;getViewData();
            $form-&gt;remove('itemCondition');
            $obj-&gt;setItemCondition($value);
</code></pre></div>

<p dir="auto"><strong>Additional context</strong></p>