0

From within a form, I have added a link which open a modal form in which I can edit a Paragraph. Here is the code for this link:

$form['my_modal_edit_form'] = [
  'title' => $this->t('Edit'),
  'url' => $this->ensureDestination($url),
  'attributes' => [
    'class' => [
      'use-ajax',
    ],
    'data-dialog-type' => 'modal',
    'data-dialog-options' => Json::encode([
      'height' => '99%',
      'width' => '95%',
    ]),
  ]
];

This is working fine except when I have added a constraint on the Paragraph being edited:

When I wrongly encode this modal form to fire the constraint, the constraint error message will not be displayed in the modal form but in a "new" form which is not modal (I mean the same form but not modal).

This could lead to a data loose in the following scenario:

1-Change a field value of field AAA in the "original" form

2-Open the modal form to edit the paragraph

3-Wrongly encode it to fire the constraint

4-A new Paragraph Edit form is displayed (the modal is removed). This means that the "original" form is "replaced" thus we lost the encoding of the AAA field.

My question is: How can I make sure that the modal is not replaced by the "not modal" same form when an error is displayed

Note: It looks like some constraints are working fine:

Working fine: A required "text" field will display some kind of popup near the missing value in the modal form

Not working: But a required "file" field will go into the problem. (A file field that you set as required in the Field UI)

6
  • There are two problems. (1) Paragraphs are designed to run inline in subforms, not in modals. (2) The file field is using its own Ajax, probably only tested in entity forms with nested field widgets and subforms.
    – 4uk4
    Commented Apr 16, 2021 at 9:04
  • regarding (1) I have created a route to handle Paragraphs in their own form (some kind of Paragraph Edit Module) and (2) ok, I assume I could replace the default "required file field" constraint by my own, but I have developed another simple constraint on the Paragraph (without Ajax) [2 fields must be the sum of 5 other fields] and I got the problem explained here.
    – Baud
    Commented Apr 16, 2021 at 9:15
  • This constraint is on entity level, not on field level like the other two. This could explain the different behavior. The question is whether paragraphs are designed to run in a separate modal. If not, even if you find a solution now, this will probably difficult to maintain in future releases.
    – 4uk4
    Commented Apr 16, 2021 at 9:33
  • ok, thus no solution I assume. Thank you!! As I am not sure that the problem is coming form the Paragraph, if I have time, I will test with a Node Form... Note also that the "Required file Field" constraint is not on the entity level.
    – Baud
    Commented Apr 16, 2021 at 9:59
  • 2
    Because file/image required shows error using the PHP status message rather than JS, it's a duplicate of How to notify user using ->messenger() and ajax
    – No Sssweat
    Commented Apr 19, 2021 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.