Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping a deeply-nested property_path to a parent form
    primarykey
    data
    text
    <p>I'm building a form in Symfony 2 whose fields will vary depending on how the corresponding entity is configured.</p> <p>Briefly, each entity has a set of "detail" fields that can hold different types and configurations of data.</p> <p>For example, a <code>Project</code> entity might have the following configuration:</p> <ul> <li><code>url</code> renders as a text input and validates as a URL with max length of 300 chars.</li> <li><code>description</code> renders as a textarea with no validation constraints.</li> <li><code>logo</code> renders as a file input and validates as an image file with max dimensions of 500x500.</li> </ul> <p>And so on. The part that makes this interesting is that all of this is configured via database tables so that an administrator could change the configuration of these models via a UI.</p> <p>The (relevant part of the) database structure looks something like this:</p> <p><img src="https://i.stack.imgur.com/Q9Mmr.png" alt="ERD showing 4 tables - project, project_detail, detail_type and detail_type_assignment"></p> <ul> <li><code>project</code> stores the Project records.</li> <li><code>project_detail</code> stores the value of each detail field for each Project.</li> <li><code>detail_type</code> defines the type and configuration for each detail field.</li> <li><code>detail_type_assignment</code> defines which detail types are available for each entity and the order in which the fields should display on forms.</li> </ul> <p>Everything is working great so far except for rendering error messages in forms.</p> <p>When any of these detail fields generates a validation error, it is displayed at the top of the form:</p> <p><img src="https://i.stack.imgur.com/FSM7x.png" alt="Screenshot of project edit form with detail field error messages displayed at the top."></p> <p>Note in the above image, "EIN" is a field that exists in the <code>Project</code> entity (i.e., implemented the normal way for a Symfony form), while "URL" and "Logo Upload" are implemented as detail fields.</p> <p>Here's what the <code>ProjectType</code> looks like:</p> <pre><code>class ProjectType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('name') -&gt;add('ein', 'ein') ; /* Add detail fields to the form builder. */ foreach($this-&gt;getDetailTypes() as $detailType) { $slug = $detailType-&gt;getSlug(); $formatter = $detailType-&gt;createFormatterInstance(''); $builder-&gt;add( $slug , $formatter-&gt;getFormFieldType() , $formatter-&gt;getFormFieldOptions() ); /* E.g., * * $builder-&gt;add( * 'url' * , 'text' * , array('label' =&gt; 'URL', ...) * ) */ } } ... } </code></pre> <p>I'm pretty sure what's going on here is that the <code>ViolationMapper</code> can't translate the <code>property_path</code> correctly.</p> <p>For example, at runtime, the <code>property_path</code> of the <code>url</code> value is <code>project.details[url].value</code>, but the field is located at <code>project.url</code>.</p> <p>I would prefer not to construct a complex form hierarchy so that the position of each field matches its <code>property_path</code>. Is there a way I can change path that violations get added to in the <code>ExecutionContext</code>?</p>
    singulars
    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.
    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