Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Annotations are great, and we're using them with code generation so that we can define form element behaviors based on the schema properties returned by the Doctrine EntityManger. I was hopeful that annotations would put more of the entity definition in one place and make them easier to manage, and so far this has been true.</p> <p>That being said, I've found annotations a bit inflexible, in that the attributes assigned by annotation, cannot be overridden with other annotations in a sub-class.</p> <p>It is easy to override attributes set with annotations at runtime, but you can't do it with more annotations. (May be stating the obvious.)</p> <p>So I'm doing overrides in my controller actions at the moment.</p> <p>Example:</p> <pre><code> $builder = new AnnotationBuilder(); $form = $builder-&gt;createForm($myEntity); // customize the the InputFilter for myElement $form-&gt;getInputFilter()-&gt;get('myElement')-&gt;setAllowEmpty(FALSE); $form-&gt;getInputFilter()-&gt;get('myElement')-&gt;setRequired(TRUE); $form-&gt;getInputFilter()-&gt;get('myElement')-&gt;getValidatorChain()-&gt;addValidator(new \Zend\Validator\NotEmpty('all')); // carry on with the form as normal $form-&gt;setData($this-&gt;getRequest()-&gt;getQuery()); </code></pre> <p>As I've only just started needing to apply custom validation rules, and expect these to get more complicated over time, and perhaps even conditional, I expect I'll want to move my form builder out of the Controllers and into the Models. The reason being, when/if I start defining validation rules that are conditional, that logic is in the Model where it belongs. This will clean up the Controller actions too, as all the form assembly gets turned into a black box method.</p> <p>Example:</p> <pre><code> $form = $myEntityModel-&gt;buildForm($myEntity); // carry on with the form as normal $form-&gt;setData($this-&gt;getRequest()-&gt;getQuery()); </code></pre> <p>So I don't think it matters whether or not you use annotations to define your default input rules. You are going to modify those according to business logic regardless of how you initially define them.</p> <p>It sounds to me like you might benefit from moving form assembly into your model classes to achieve your goal of keeping validation rules entirely separated from the entity. I believe your instinct is correct that business logic needs to stay in the models, not in the controllers or entities.</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.
    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