You are here

class AutosaveFormValidator in Autosave Form 8

Provides validation of form submissions with AutosaveForm enabled.

Hierarchy

Expanded class hierarchy of AutosaveFormValidator

1 string reference to 'AutosaveFormValidator'
autosave_form.services.yml in ./autosave_form.services.yml
autosave_form.services.yml
1 service uses AutosaveFormValidator
form_validator.autosave_form in ./autosave_form.services.yml
\Drupal\autosave_form\Form\AutosaveFormValidator

File

src/Form/AutosaveFormValidator.php, line 17

Namespace

Drupal\autosave_form\Form
View source
class AutosaveFormValidator extends FormValidator {
  use AutosaveButtonClickedTrait;

  /**
   * The form validator service.
   *
   * @var \Drupal\Core\Form\FormValidatorInterface
   */
  protected $formValidator;

  /**
   * Constructs a AutosaveFormValidator object.
   *
   * @param \Drupal\Core\Form\FormValidatorInterface $form_validator
   *   The form validator service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
   *   The CSRF token generator.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Form\FormErrorHandlerInterface $form_error_handler
   *   The form error handler.
   */
  public function __construct(FormValidatorInterface $form_validator, RequestStack $request_stack, TranslationInterface $string_translation, CsrfTokenGenerator $csrf_token, LoggerInterface $logger, FormErrorHandlerInterface $form_error_handler) {
    $this->formValidator = $form_validator;
    parent::__construct($request_stack, $string_translation, $csrf_token, $logger, $form_error_handler);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm($form_id, &$form, FormStateInterface &$form_state) {
    $autosave_submission = $this
      ->isAutosaveTriggered($form_state);
    if ($autosave_submission) {

      // On subsequent autosaving we don't need to execute the form validation
      // as we are not going to build the intermediate entity. However it might
      // happen that between here and the autosave submission handler the
      // autosaved state has been purged and therefore we have to check
      // explicitly for that there instead of building the intermediate entity.
      $form_state
        ->setTemporaryValue('autosave_form_form_validation_skipped', TRUE);
    }
    else {

      // We have to execute the validation in the case of autosave submission
      // for the very first time as in this case we'll build the intermediate
      // entity for comparison and some input values are being prepared in the
      // validate functions. This is the case with e.g. autocomplete for entity
      // references.
      $this->formValidator
        ->validateForm($form_id, $form, $form_state);
    }

    // In order for the autosave submit callback to be executed we have to
    // clear the errors caused from the validation, otherwise no submit
    // callbacks will be executed.
    if ($autosave_submission && $form_state::hasAnyErrors()) {
      $form_state
        ->clearErrors();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutosaveButtonClickedTrait::isAutosaveTriggered protected function Checks if the submission is triggered by autosave save.
AutosaveButtonClickedTrait::isRejectTriggered protected function Checks if autosave restore has been triggered.
AutosaveButtonClickedTrait::isRestoreTriggered protected function Checks if autosave restore has been triggered.
AutosaveFormValidator::$formValidator protected property The form validator service.
AutosaveFormValidator::validateForm public function Validates user-submitted form data in the $form_state. Overrides FormValidator::validateForm
AutosaveFormValidator::__construct public function Constructs a AutosaveFormValidator object. Overrides FormValidator::__construct
FormValidator::$csrfToken protected property The CSRF token generator to validate the form token.
FormValidator::$formErrorHandler protected property The form error handler.
FormValidator::$logger protected property A logger instance.
FormValidator::$requestStack protected property The request stack.
FormValidator::determineLimitValidationErrors protected function Determines if validation errors should be limited.
FormValidator::doValidateForm protected function Performs validation on form elements.
FormValidator::executeValidateHandlers public function Executes custom validation handlers for a given form. Overrides FormValidatorInterface::executeValidateHandlers
FormValidator::finalizeValidation protected function Finalizes validation.
FormValidator::handleErrorsWithLimitedValidation protected function Handles validation errors for forms with limited validation.
FormValidator::performRequiredValidation protected function Performs validation of elements that are not subject to limited validation.
FormValidator::setInvalidTokenError public function Sets a form_token error on the given form state. Overrides FormValidatorInterface::setInvalidTokenError
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.