/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); // Ensure numeric values. if ($form_state->hasValue('weight') && !is_numeric($form_state->getValue('weight'))) { $form_state->setErrorByName('weight', $this->t('Weight value must be numeric.')); } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $source = $form_state->getValue(array('redirect_source', 0)); $redirect = $form_state->getValue(array('redirect_redirect', 0)); if ($source['path'] == '<front>') { $form_state->setErrorByName('redirect_source', t('It is not allowed to create a redirect from the front page.')); } if (strpos($source['path'], '#') !== FALSE) { $form_state->setErrorByName('redirect_source', t('The anchor fragments are not allowed.')); } if (strpos($source['path'], '/') === 0) { $form_state->setErrorByName('redirect_source', t('The url to redirect from should not start with a forward slash (/).')); } try { $source_url = Url::fromUri('internal:/' . $source['path']); $redirect_url = Url::fromUri($redirect['uri']); // It is relevant to do this comparison only in case the source path has // a valid route. Otherwise the validation will fail on the redirect path // being an invalid route. if ($source_url->toString() == $redirect_url->toString()) { $form_state->setErrorByName('redirect_redirect', t('You are attempting to redirect the page to itself. This will result in an infinite loop.')); } } catch (\InvalidArgumentException $e) { // Do nothing, we want to only compare the resulting URLs. } $parsed_url = UrlHelper::parse(trim($source['path'])); $path = isset($parsed_url['path']) ? $parsed_url['path'] : NULL; $query = isset($parsed_url['query']) ? $parsed_url['query'] : NULL; $hash = Redirect::generateHash($path, $query, $form_state->getValue('language')[0]['value']); // Search for duplicate. $redirects = \Drupal::entityManager()->getStorage('redirect')->loadByProperties(array('hash' => $hash)); if (!empty($redirects)) { $redirect = array_shift($redirects); if ($this->entity->isNew() || $redirect->id() != $this->entity->id()) { $form_state->setErrorByName('redirect_source', t('The source path %source is already being redirected. Do you want to <a href="@edit-page">edit the existing redirect</a>?', array('%source' => $source['path'], '@edit-page' => $redirect->url('edit-form')))); } } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $message = parent::validateForm($form, $form_state); // Check if flood control has been activated for sending emails. if (!$this->currentUser()->hasPermission('administer contact forms') && (!$message->isPersonal() || !$this->currentUser()->hasPermission('administer users'))) { $limit = $this->config('contact.settings')->get('flood.limit'); $interval = $this->config('contact.settings')->get('flood.interval'); if (!$this->flood->isAllowed('contact', $limit, $interval)) { $form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', array('%limit' => $limit, '@interval' => $this->dateFormatter->formatInterval($interval)))); } } return $message; }
public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $file_upload = $this->getRequest()->files->get('files[upload_pdf]', NULL, TRUE); if ($file_upload) { $this->validatePdfUpload($form, $form_state, $file_upload, 'upload_pdf'); } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $entity = parent::validateForm($form, $form_state); if ($entity->isNew()) { $exists = $this->blockContentStorage->loadByProperties(array('info' => $form_state->getValue(['info', 0, 'value']))); if (!empty($exists)) { $form_state->setErrorByName('info', $this->t('A block with description %name already exists.', array('%name' => $form_state->getValue(array('info', 0, 'value'))))); } } return $entity; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $mail = $form_state->getValue(array('mail', 0, 'value')); // Users should login to manage their subscriptions. if (\Drupal::currentUser()->isAnonymous() && ($user = user_load_by_mail($mail))) { $message = $user->isBlocked() ? $this->t('The email address %mail belongs to a blocked user.', array('%mail' => $mail)) : $this->t('There is an account registered for the e-mail address %mail. Please log in to manage your newsletter subscriptions.', array('%mail' => $mail)); $form_state->setErrorByName('mail', $message); } // Unless the submit handler is 'update', if the newsletter checkboxes are // available, at least one must be checked. $update = in_array('::submitUpdate', $form_state->getSubmitHandlers()); if (!$update && !$this->getSubscriptionWidget($form_state)->isHidden() && !count($form_state->getValue('subscriptions'))) { $form_state->setErrorByName('subscriptions', t('You must select at least one newsletter.')); } parent::validateForm($form, $form_state); }