You are here

public function ResponsiveImageStyleForm::validateForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/responsive_image/src/ResponsiveImageStyleForm.php \Drupal\responsive_image\ResponsiveImageStyleForm::validateForm()
  2. 9 core/modules/responsive_image/src/ResponsiveImageStyleForm.php \Drupal\responsive_image\ResponsiveImageStyleForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

core/modules/responsive_image/src/ResponsiveImageStyleForm.php, line 220

Class

ResponsiveImageStyleForm
Form controller for the responsive image edit/add forms.

Namespace

Drupal\responsive_image

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Only validate on edit.
  if ($form_state
    ->hasValue('keyed_styles')) {

    // Check if another breakpoint group is selected.
    if ($form_state
      ->getValue('breakpoint_group') != $form_state
      ->getCompleteForm()['breakpoint_group']['#default_value']) {

      // Remove the image style mappings since the breakpoint ID has changed.
      $form_state
        ->unsetValue('keyed_styles');
    }

    // Check that at least 1 image style has been selected when using sizes.
    foreach ($form_state
      ->getValue('keyed_styles') as $breakpoint_id => $multipliers) {
      foreach ($multipliers as $multiplier => $image_style_mapping) {
        if ($image_style_mapping['image_mapping_type'] === 'sizes') {
          if (empty($image_style_mapping['sizes'])) {
            $form_state
              ->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes'], 'Provide a value for the sizes attribute.');
          }
          if (empty(array_keys(array_filter($image_style_mapping['sizes_image_styles'])))) {
            $form_state
              ->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes_image_styles'], 'Select at least one image style.');
          }
        }
      }
    }
  }
}