Skip to content
Snippets Groups Projects

Add form validation when previewing acces changes

Merged Mario Vercellotti requested to merge issue/access_by_taxonomy-3479623:1.0.x into 1.0.x
2 unresolved threads

This PR fixes:

taking into consideration form validation

(bonus) Switching to using the StringTranslation trait instead of the translate method of the translation manager, since that should not be used directly, check https://github.com/drupal/drupal/blob/4b25dc769526998569fdced5ffbdf0100584c5c2/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php#L15

Edited by Mario Vercellotti

Merge request reports

Merge train pipeline #308939 passed with warnings

Merge train pipeline passed with warnings for 47afec72

Merged by federico pratofederico prato 5 months ago (Oct 14, 2024 5:19am UTC)

Loading

Pipeline #308943 passed with warnings

Pipeline passed with warnings for 47afec72 on 1.0.x

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
585 $form_state->clearErrors();
586 $response->addCommand(
587 new MessageCommand(
588 $access_by_taxonomy_service->translationManager->translate('To preview access changes, please fix these validation errors: <strong>:errors</strong>', [':errors' => implode(' ', $errors)]),
589 '#access_by_taxonomy_status_messages',
590 ['type' => 'warning'],
591 TRUE
592 )
593 );
594
595 // Since we validated the form, and there were errors, we need to delete
596 // error messages, because we don't want them to be displayed twice
597 // when the user reloads the page or navigates away from the page:
598 /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
599 $messenger = \Drupal::service('messenger');
600 $messenger->deleteByType('error');
  • added 1 commit

    • 099da08b - Use string translation trait instead of translation manager

    Compare with previous version

  • Mario Vercellotti changed the description

    changed the description

  • added 1 commit

    • e393d469 - Use t instead of the translate method of translation manager

    Compare with previous version

  • Mario Vercellotti changed the description

    changed the description

  • added 1 commit

    • 358006d6 - Fix form validation logic in access preview

    Compare with previous version

  • 574 // form and test form state.
    575 $test_form = $form;
    576 $test_form_state = $form_state;
    574 577
    575 578 /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
    576 $form_object = $form_state->getFormObject();
    579 $form_object = $test_form_state->getFormObject();
    580
    581 try {
    582 // Validate the form:
    583 $form_object->validateForm($test_form, $test_form_state);
    584 }
    585 // Validation could trigger more errors depending on special validation
    586 // needed for some of the fields in the form. We are not interested in those
    587 // errors, so we catch them and ignore them.
    588 catch (\Exception) {
    • Without this, some fields were triggering additional errors in this context:

      1. date field -> "the datetime field must be a string"

      etc..

      This way the basic validation works... I don't know if there's a more advanced way to handle this.

    • Please register or sign in to reply
  • federico prato approved this merge request

    approved this merge request

  • Please register or sign in to reply
    Loading