public function AutosaveFormValidator::validateForm in Autosave Form 8
Validates user-submitted form data in the $form_state.
Parameters
$form_id: A unique string identifying the form for validation, submission, theming, and hook_form_alter functions.
$form: An associative array containing the structure of the form, which is passed by reference. Form validation handlers are able to alter the form structure (like #process and #after_build callbacks during form building) in case of a validation error. If a validation handler alters the form structure, it is responsible for validating the values of changed form elements in $form_state->getValues() to prevent form submit handlers from receiving unvalidated values.
$form_state: The current state of the form. The current user-submitted data is stored in $form_state->getValues(), though form validation functions are passed an explicit copy of the values for the sake of simplicity. Validation handlers can also use $form_state to pass information on to submit handlers. For example: $form_state->set('data_for_submission', $data); This technique is useful when validation requires file parsing, web service requests, or other expensive requests that should not be repeated in the submission step.
Overrides FormValidator::validateForm
File
- src/
Form/ AutosaveFormValidator.php, line 52
Class
- AutosaveFormValidator
- Provides validation of form submissions with AutosaveForm enabled.
Namespace
Drupal\autosave_form\FormCode
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();
}
}