validateOptionsForm example

foreach ($this->view->displayHandlers as $id => $display) {
            if ($id != $this->view->current_display && ($form_state->getValue('display_id') == $id || (isset($display->new_id) && $form_state->getValue('display_id') == $display->new_id))) {
              $form_state->setError($form['display_id']$this->t('Display id should be unique.'));
            }
          }
        }
        break;

      case 'query':
        if ($this->view->query) {
          $this->view->query->validateOptionsForm($form['query']$form_state);
        }
        break;
    }

    // Validate plugin options. Every section with "_options" in it, belongs to     // a plugin type, like "style_options".     if (str_contains($section, '_options')) {
      $plugin_type = str_replace('_options', '', $section);
      // Load the plugin and let it handle the validation.       if ($plugin = $this->getPlugin($plugin_type)) {
        $plugin->validateOptionsForm($form[$section]$form_state);
      }
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    $option_values = &$form_state->getValue('options');
    if (empty($option_values)) {
      return;
    }

    // Let the plugins do validation.     $default_id = $option_values['default_argument_type'];
    $plugin = $this->getPlugin('argument_default', $default_id);
    if ($plugin) {
      $plugin->validateOptionsForm($form['argument_default'][$default_id]$form_state$option_values['argument_default'][$default_id]);
    }

    // summary plugin     $summary_id = $option_values['summary']['format'];
    $plugin = $this->getPlugin('style', $summary_id);
    if ($plugin) {
      $plugin->validateOptionsForm($form['summary']['options'][$summary_id]$form_state$option_values['summary']['options'][$summary_id]);
    }

    $sanitized_id = $option_values['validate']['type'];
    // Correct ID for js sanitized version.
if ($save_ui_cache) {
      $view->cacheSet();
    }

    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_state->get('handler')->validateOptionsForm($form['options']$form_state);

    if ($form_state->getErrors()) {
      $form_state->set('rerender', TRUE);
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $view = $form_state->get('view');
    
$name = $form_state->get('update_name');
    $view->getStandardButtons($form$form_state, 'views_ui_edit_display_form', $name);
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $view = $form_state->get('view');
    $display_id = $form_state->get('display_id');
    $view->getExecutable()->displayHandlers->get($display_id)->validateOptionsForm($form['options']$form_state);

    if ($form_state->getErrors()) {
      $form_state->set('rerender', TRUE);
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $view = $form_state->get('view');
    
'#description' => $this->t('This is a textfield for test_option.'),
          '#default_value' => $this->getOption('test_option'),
        ];
        break;
    }
  }

  /** * {@inheritdoc} */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    parent::validateOptionsForm($form$form_state);
    \Drupal::logger('views')->notice($form_state->getValue('test_option'));
    switch ($form_state->get('section')) {
      case 'test_option':
        if (!trim($form_state->getValue('test_option'))) {
          $form_state->setError($form['test_option']$this->t('You cannot have an empty option.'));
        }
        break;
    }
  }

  /** * {@inheritdoc} */
$form['use_admin_theme']['#default_value'] = TRUE;
          $form['use_admin_theme']['#attributes'] = ['disabled' => 'disabled'];
        }
        break;
    }
  }

  /** * {@inheritdoc} */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    parent::validateOptionsForm($form$form_state);

    if ($form_state->get('section') == 'menu') {
      $path = $this->getOption('path');
      $menu_type = $form_state->getValue(['menu', 'type']);
      if ($menu_type == 'normal' && str_contains($path, '%')) {
        $form_state->setError($form['menu']['type']$this->t('Views cannot create normal menu links for paths with a % in them.'));
      }

      if ($menu_type == 'default tab' || $menu_type == 'tab') {
        $bits = explode('/', $path);
        $last = array_pop($bits);
        
// Account for the leading backslash.           '#maxlength' => 254,
        ];
        break;
    }
  }

  /** * {@inheritdoc} */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    parent::validateOptionsForm($form$form_state);

    if ($form_state->get('section') == 'path') {
      $errors = $this->validatePath($form_state->getValue('path'));
      foreach ($errors as $error) {
        $form_state->setError($form['path']$error);
      }

      // Automatically remove '/' and trailing whitespace from path.       $form_state->setValue('path', trim($form_state->getValue('path'), '/ '));
    }
  }

  
'#options' => [
          'date' => $this->t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
          'offset' => $this->t('An offset from the current time such as "@example1" or "@example2"', ['@example1' => '+1 day', '@example2' => '-2 hours -30 minutes']),
        ],
        '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
      ];
    }
    parent::valueForm($form$form_state);
  }

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

    if (!empty($this->options['exposed']) && $form_state->isValueEmpty(['options', 'expose', 'required'])) {
      // Who cares what the value is if it's exposed and non-required.       return;
    }

    $this->validateValidTime($form['value']$form_state$form_state->getValue(['options', 'operator'])$form_state->getValue(['options', 'value']));
  }

  public function validateExposed(&$form, FormStateInterface $form_state) {
    if (empty($this->options['exposed'])) {
      
'#options' => $this->getBulkOptions(FALSE),
      '#default_value' => $this->options['selected_actions'],
    ];

    parent::buildOptionsForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    parent::validateOptionsForm($form$form_state);

    $selected_actions = $form_state->getValue(['options', 'selected_actions']);
    $form_state->setValue(['options', 'selected_actions']array_values(array_filter($selected_actions)));
  }

  /** * {@inheritdoc} */
  public function preRender(&$values) {
    parent::preRender($values);

    
Home | Imprint | This part of the site doesn't use cookies.