Symfony - entity inheritance and form - symfony

In my project I have an abstract entity, let's call it Parent, and two child entities: ChildA and ChildB that extend Parent class. I'm using doctrine and a single table strategy, has ChildA and ChildB are similiar. This part is working ok, now my problem is with the form.
I want to have a single form that can be used to create an entity of one of those classes (ChildA or ChildB), so I want to have a first field in the form to select which kind of entity the user wants to create, and show the fields for that class (has there are only one different field, I'm using javascript to show/hide the field according to the selected class)
To accomplish this I have created a form with all the fields of both ChildA and ChildB plus the field to select the type, and my idea was in controller check the type, and then create a specific form associated with ChildA or ChildB according to the selected type, and bind it with the valus received from the main form, but the problem here is how to display the errors in this form
Anyone have a good solution for this problem?

I think you make it very difficult this way.
For this problem i would create 2 forms (FormChildA, FormChildB) with the associated fields accordingly.
Because you are using javascript anyway, just render the page with a choice and get the form with ajax:
<div id="select-type">
<button value="child_a" type="button">Select ChildA</button>
<button value="child_b" type="button">Select ChildB</button>
</div>
<div id="form-container"></div>
<script>
$('#select-type button').on('click', function(event) {
event.preventDefault();
$.get('path/to/get_ajax_form', {type: $(this).val()}, function(data) {
$('#form-container').html(data);
});
});
</script>
Create a Controller method to retrieve the form:
public function getAjaxFormAction()
{
$type = $this->get('request')->query->get('type');
switch( $type ) {
case 'child_a':
$form = $this->createForm(new FormChildA, new ChildA);
break;
case 'child_b':
$form = $this->createForm(new FormChildB, new ChildB);
break;
}
return $this->render('AcmeBundle:Forms:_type_form.html.twig', array(
'form' => $form->createView(),
'type' => $type,
));
}
Add to each form a hidden field with the form type value,
this way you can validate these forms in one method (same way as you retrieve them).
This makes it easier to modify and validate each form separately!

Related

How can I add a paragraph (as default) in the node form by using hook_form_alter in Drupal 8?

I am trying to add a bunch of different empty paragraphs of different types, to a entity reference revisions field, everytime a node of a certain content type is created.
I DON'T want to use the contrib module "default paragraphs" for this, because I need to use a certain form widget here, and default paragraphs is also achieved by a widget.
What I tried so far:
function myModule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
$paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
'type' => 'tab_features'
]);
$paragraph->save();
$form['field_tabs']['widget'][0]['target_id']=$paragraph->id();
$form['field_tabs']['widget'][0]['target_revision_id']=$paragraph->getRevisionId();
return $form;
}
$field_tabs is my entity reference revisions field.
'tab_features' is the paragraphs type I want to add.
I guess there should be a method that can be used in the form or form widget to add a paragraph to the form, like someone already clicked the button to add it. I want to avoid to actually trigger this via Javascript if possible. Anybody knows how to do this in form_alter?
In a project I'm working on, we have done something like this:
//get the body field
$field = $entity->get('field_em_p_body');
$paragraph = Paragraph::create([
'type' => 'em_section', // Paragraph type.
]);
$paragraph->isNew();
$paragraph->set('YOUR_FIELD', 'SOMETHING');
$field->appendItem($paragraph);

Symfony - List of form fields in event

Is possible to get some list of fields in FormEvents::PRE_SET_DATA?
I need edit entity which I put to Form by Event. Entity contains PersistCollection which I need transform to ArrayObject.
I would like created on automatic for many entities. I need list of fields (names) for data mapping.
My idea:
$fields = $event->getFormFields();
foreach ($fields as $field) {
dump($field); --> return 'name'
}
It's not completely clear what you're trying to achieve, but yes, you can get all child forms from parent form easily:
You can either use:
foreach ($event->getForm()->all() as $childForm) {
// ...
}
or, since Symfony Form implements IteratorAggregate interface:
foreach ($event->getForm() as $childForm) {
}

Get the value of the selected field in Drupal Form API

I have a custom content type called events which has a few fields defined in it.
The field name is field_store_name. I can get all the options from these check boxes using this code:
$form['field_store']['und']['#options']
This is how I get the option(s) that are selected/checked. Is this the correct way of doing this?
$form_state['build_info']['args']['0']->field_store['und']
Thanks
When user submits form your custom submitter could be called.
To add custom form submitter to any form you should use:
/* Implements hook_form_alter(). */
function moduleName_form_alter($form, $form_state) {
// ...
$form['#submit'][] = 'moduleName_submitterName';
// ...
}
So in custom submitter you will have all submitted values under $form_state['values']:
function moduleName_submitterName($form, $form_state) {
dpm($form_state['values']);
}
This index will apper in $form_state array only when you submit form and will contain submitted values. $form array will still contain default values shown at form before you've changed them and submitted form.
Read more:
An example of form submitter: https://www.drupal.org/node/717740.
hook_form_alter(): https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7
Value you need should be $form_state['values']['field_store]['und'][0].

Symfony Form from other Controller

I have a view that shows the attributes of my parent entity. This Entity will have other child entities. I want the create form for those childs to be placed in my show.html.twig of the parent.
How do I place the create form of those childs in my parents view? How do I combine two controllers of two different entities?
You can create forms for any entity in any controller. Since you didn't post any code it's hard to guess how your controller looks like, but here an example that might help:
public function showMainEntityAction(MainEntity $mainEntity){
...
$childEntity = new ChildEntity();
$childEntity->setMainEntity($mainEntity);
$childEntityForm = $this->createForm(new ChildEntityType(), $childEntity);
...
return $this->render('...show.twig.html', array(
'entity' => $mainEntity,
'childEntityForm' => $childEntityForm
);
}
Of course you have to define a form type for the child entities and add form_widget(childEntityForm) to your template.

Conditionally disable validation for embedded form

I have an embedded form (for Address) which has its own validations for various properties. I embed this form in a parent form (for Person), and I have a checkbox on the parent form that says something like "Person has an address?"
When the checkbox is left unchecked, I want to disable all the validation for the embedded Address form. Or, better yet, if I can just remove the embedded form from being submitted completely that would be OK too.
I looked at using validation groups, but the use case doesn't match my own.
OK, figured this out. When adding the AddressType embedded form in my form builder, I just pass in the option for validation groups like so:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->add('address', new AddressType(), array(
'label' => 'Address',
'validation_groups' => function (FormInterface $form) {
if ($form->getParent()->get('toggleAddress')->getData() === false) {
return array();
}
return array('Default');
}
));
});
Within the validation group function, a check is made to see if the toggle to enable Address is off. If so, return a blank array, with removes all validation groups, including the "Default" one.
You try to fix your issue with validation group which will not cover your use case (it can but it will be tricky because en empty Address object will be linked to your Person object).
Basically, you embed your Address form everytime whereas it should only be embed when the checkbox is checked. IMHO, you should rely on dynamic form as explained here.
With this solution, you will need extra JS code in order to update you form when you click the checkbox in order to update the whole form accordingly. Then, there will be no issue about validation because the Address object will only be created when the form is embed.
Additionally (just for information), you can add/edit validation groups according to the submitted data as explained here.
Hope my answer is helpfull!

Resources