Commit 7fff6e85 authored by alexpott's avatar alexpott

Issue #2297875 by herom, bfr: Replace calls to form_set_error() to $form_state->setErrorByName().

parent 92ff3876
......@@ -135,18 +135,6 @@ function form_execute_handlers($type, &$form, FormStateInterface $form_state) {
}
}
/**
* Files an error against a form element.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use $form_state->setErrorByName().
*
* @see \Drupal\Core\Form\FormStateInterface::setErrorByName().
*/
function form_set_error($name, FormStateInterface $form_state, $message = '') {
$form_state->setErrorByName($name, $message);
}
/**
* Returns an associative array of all errors.
*
......
......@@ -159,7 +159,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!valid_email_address($form_state->getValue('recipient')) && strpos($form_state->getValue('recipient'), ':mail') === FALSE) {
// We want the literal %author placeholder to be emphasized in the error message.
form_set_error('recipient', $form_state, t('Enter a valid email address or use a token email address such as %author.', array('%author' => '[node:author:mail]')));
$form_state->setErrorByName('recipient', t('Enter a valid email address or use a token email address such as %author.', array('%author' => '[node:author:mail]')));
}
}
......
......@@ -334,7 +334,7 @@ function color_scheme_form_validate($form, FormStateInterface $form_state) {
// Only accept hexadecimal CSS color strings to avoid XSS upon use.
foreach ($form_state->getValue('palette') as $key => $color) {
if (!color_valid_hexadecimal_string($color)) {
form_set_error('palette][' . $key, $form_state, t('You must enter a valid hexadecimal color value for %name.', array('%name' => $form['color']['palette'][$key]['#title'])));
$form_state->setErrorByName('palette][' . $key, t('You must enter a valid hexadecimal color value for %name.', array('%name' => $form['color']['palette'][$key]['#title'])));
}
}
}
......
......@@ -264,7 +264,7 @@ function content_translation_form_language_content_settings_validate(array $form
$translatable_fields = isset($settings[$entity_type][$bundle]['fields']) ? array_filter($settings[$entity_type][$bundle]['fields']) : FALSE;
if (empty($translatable_fields)) {
$t_args = array('%bundle' => $form['settings'][$entity_type][$bundle]['settings']['#label']);
form_set_error($name, $form_state, t('At least one field needs to be translatable to enable %bundle for translation.', $t_args));
$form_state->setErrorByName($name, t('At least one field needs to be translatable to enable %bundle for translation.', $t_args));
}
$values = $bundle_settings['settings']['language'];
......@@ -272,7 +272,7 @@ function content_translation_form_language_content_settings_validate(array $form
foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $language) {
$locked_languages[] = $language->name;
}
form_set_error($name, $form_state, t('Translation is not supported if language is always one of: @locked_languages', array('@locked_languages' => implode(', ', $locked_languages))));
$form_state->setErrorByName($name, t('Translation is not supported if language is always one of: @locked_languages', array('@locked_languages' => implode(', ', $locked_languages))));
}
}
}
......
......@@ -753,7 +753,7 @@ function content_translation_language_configuration_element_validate($element, F
// @todo Set the correct form element name as soon as the element parents
// are correctly set. We should be using NestedArray::getValue() but for
// now we cannot.
form_set_error('', $form_state, t('"Show language selector" is not compatible with translating content that has default language: %choice. Either do not hide the language selector or pick a specific language.', array('%choice' => $locked_languages[$values['langcode']])));
$form_state->setErrorByName('', t('"Show language selector" is not compatible with translating content that has default language: %choice. Either do not hide the language selector or pick a specific language.', array('%choice' => $locked_languages[$values['langcode']])));
}
}
......
......@@ -420,11 +420,11 @@ function entityFormValidate($form, FormStateInterface $form_state) {
$translation = $form_state->getValue('content_translation');
// Validate the "authored by" field.
if (!empty($translation['name']) && !($account = user_load_by_name($translation['name']))) {
form_set_error('content_translation][name', $form_state, t('The translation authoring username %name does not exist.', array('%name' => $translation['name'])));
$form_state->setErrorByName('content_translation][name', t('The translation authoring username %name does not exist.', array('%name' => $translation['name'])));
}
// Validate the "authored on" field.
if (!empty($translation['created']) && strtotime($translation['created']) === FALSE) {
form_set_error('content_translation][created', $form_state, t('You have to specify a valid translation authoring date.'));
$form_state->setErrorByName('content_translation][created', t('You have to specify a valid translation authoring date.'));
}
}
}
......
......@@ -207,7 +207,7 @@ function editor_form_filter_admin_format_validate($form, FormStateInterface $for
// selected a text editor and has then clicked 'Save configuration', we should
// point out that the user must still configure the text editor.
if ($form_state->getValue(array('editor', 'editor')) !== '' && empty($form_state['editor'])) {
form_set_error('editor][editor', $form_state, t('You must configure the selected text editor.'));
$form_state->setErrorByName('editor][editor', t('You must configure the selected text editor.'));
}
}
......
......@@ -1446,7 +1446,7 @@ function file_managed_file_save_upload($element, FormStateInterface $form_state)
$destination = isset($element['#upload_location']) ? $element['#upload_location'] : NULL;
if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
\Drupal::logger('file')->notice('The upload directory %directory for the file field !name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array('%directory' => $destination, '!name' => $element['#field_name']));
form_set_error($upload_name, $form_state, t('The file could not be uploaded.'));
$form_state->setErrorByName($upload_name, t('The file could not be uploaded.'));
return FALSE;
}
......@@ -1456,7 +1456,7 @@ function file_managed_file_save_upload($element, FormStateInterface $form_state)
if ($files_uploaded) {
if (!$files = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
\Drupal::logger('file')->notice('The file upload failed. %upload', array('%upload' => $upload_name));
form_set_error($upload_name, $form_state, t('Files in the !name field were unable to be uploaded.', array('!name' => $element['#title'])));
$form_state->setErrorByName($upload_name, t('Files in the !name field were unable to be uploaded.', array('!name' => $element['#title'])));
return array();
}
......
......@@ -213,7 +213,7 @@ function forum_node_validate(EntityInterface $node, $form, FormStateInterface $f
}
$term = $item->entity;
if (!$term) {
form_set_error('taxonomy_forums', $form_state, t('Select a forum.'));
$form_state->setErrorByName('taxonomy_forums', t('Select a forum.'));
continue;
}
$used = \Drupal::entityQuery('taxonomy_term')
......@@ -223,7 +223,7 @@ function forum_node_validate(EntityInterface $node, $form, FormStateInterface $f
->count()
->execute();
if ($used && !empty($term->forum_container->value)) {
form_set_error('taxonomy_forums', $form_state, t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->getName())));
$form_state->setErrorByName('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->getName())));
}
}
}
......
......@@ -129,7 +129,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) {
form_set_error('bgcolor', $form_state, $this->t('Background color must be a hexadecimal color value.'));
$form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.'));
}
}
......
......@@ -86,7 +86,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
if ($form_state->isValueEmpty('width') && $form_state->isValueEmpty('height')) {
form_set_error('data', $form_state, $this->t('Width and height can not both be blank.'));
$form_state->setErrorByName('data', $this->t('Width and height can not both be blank.'));
}
}
......
......@@ -501,7 +501,7 @@ function menu_ui_form_node_type_form_validate(&$form, FormStateInterface $form_s
if (count($available_menus)) {
$menu_item_id_parts = explode(':', $form_state->getValue('menu_parent'));
if (!in_array($menu_item_id_parts[0], $available_menus)) {
form_set_error('menu_parent', $form_state, t('The selected menu item is not under one of the selected menus.'));
$form_state->setErrorByName('menu_parent', t('The selected menu item is not under one of the selected menus.'));
}
}
else {
......
......@@ -405,7 +405,7 @@ function hook_node_update_index(\Drupal\node\NodeInterface $node, $langcode) {
* finished editing the node and is previewing or submitting it. It is invoked
* at the end of all the standard validation steps.
*
* To indicate a validation error, use form_set_error().
* To indicate a validation error, use $form_state->setErrorByName().
*
* Note: Changes made to the $node object within your hook implementation will
* have no effect. The preferred method to change a node's content is to use
......@@ -424,7 +424,7 @@ function hook_node_update_index(\Drupal\node\NodeInterface $node, $langcode) {
function hook_node_validate(\Drupal\node\NodeInterface $node, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
if (isset($node->end) && isset($node->start)) {
if ($node->start > $node->end) {
form_set_error('time', $form_state, t('An event may not end before it starts.'));
$form_state->setErrorByName('time', t('An event may not end before it starts.'));
}
}
}
......
......@@ -121,7 +121,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$exists = (bool) $this->connection->queryRange('SELECT 1 FROM {users_field_data} WHERE name = :name AND default_langcode = 1', 0, 1, array(':name' => $form_state->getValue('owner_name')))->fetchField();
if (!$exists) {
form_set_error('owner_name', $form_state, t('Enter a valid username.'));
$form_state->setErrorByName('owner_name', t('Enter a valid username.'));
}
}
......
......@@ -128,7 +128,7 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
// If array filter returns empty, no values have been entered. Unique keys
// should only be validated if we have some.
if (($filtered = array_filter($aliases)) && (array_unique($filtered) !== $filtered)) {
form_set_error('aliases', $form_state, t('All field aliases must be unique'));
$form_state->setErrorByName('aliases', t('All field aliases must be unique'));
}
}
......
......@@ -108,7 +108,7 @@ public function validateExposed(&$form, FormStateInterface $form_state) {
if (!$form_state->isValueEmpty($key)) {
$this->queryParseSearchExpression($form_state->getValue($key));
if (count($this->searchQuery->words()) == 0) {
form_set_error($key, $form_state, format_plural(\Drupal::config('search.settings')->get('index.minimum_word_size'), 'You must include at least one positive keyword with 1 character or more.', 'You must include at least one positive keyword with @count characters or more.'));
$form_state->setErrorByName($key, format_plural(\Drupal::config('search.settings')->get('index.minimum_word_size'), 'You must include at least one positive keyword with 1 character or more.', 'You must include at least one positive keyword with @count characters or more.'));
}
}
}
......
......@@ -294,7 +294,7 @@ protected function emptySelectedMessage() {
public function viewsFormValidate(&$form, FormStateInterface $form_state) {
$selected = array_filter($form_state->getValue($this->options['id']));
if (empty($selected)) {
form_set_error('', $form_state, $this->emptySelectedMessage());
$form_state->setErrorByName('', $this->emptySelectedMessage());
}
}
......
......@@ -8,6 +8,7 @@
use Drupal\Core\Cache\Cache;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
......@@ -1090,8 +1091,10 @@ function system_preprocess_block(&$variables) {
*
* @param $form_element
* The form element containing the name of the directory to check.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function system_check_directory($form_element) {
function system_check_directory($form_element, FormStateInterface $form_state) {
$directory = $form_element['#value'];
if (strlen($directory) == 0) {
return $form_element;
......@@ -1100,13 +1103,13 @@ function system_check_directory($form_element) {
$logger = \Drupal::logger('file system');
if (!is_dir($directory) && !drupal_mkdir($directory, NULL, TRUE)) {
// If the directory does not exists and cannot be created.
form_set_error($form_element['#parents'][0], $form_state, t('The directory %directory does not exist and could not be created.', array('%directory' => $directory)));
$form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory does not exist and could not be created.', array('%directory' => $directory)));
$logger->error('The directory %directory does not exist and could not be created.', array('%directory' => $directory));
}
if (is_dir($directory) && !is_writable($directory) && !drupal_chmod($directory)) {
// If the directory is not writable and cannot be made so.
form_set_error($form_element['#parents'][0], $form_state, t('The directory %directory exists but is not writable and could not be made writable.', array('%directory' => $directory)));
$form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory exists but is not writable and could not be made writable.', array('%directory' => $directory)));
$logger->error('The directory %directory exists but is not writable and could not be made writable.', array('%directory' => $directory));
}
elseif (is_dir($directory)) {
......
......@@ -46,7 +46,7 @@ public function validateName(&$element, FormStateInterface $form_state) {
drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state->getValue('name'))));
// Trigger a form validation error to see our changes.
form_set_error('', $form_state);
$form_state->setErrorByName('');
}
}
......
......@@ -90,7 +90,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state->isValueEmpty('textfield')) {
form_set_error('textfield', $form_state, t('Textfield is required.'));
$form_state->setErrorByName('textfield', t('Textfield is required.'));
}
}
......
......@@ -69,7 +69,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state->getValue('name'))));
// Trigger a form validation error to see our changes.
form_set_error('', $form_state);
$form_state->setErrorByName('');
}
}
......
......@@ -507,8 +507,8 @@ function user_validate_current_pass(&$form, FormStateInterface $form_state) {
if ((strlen(trim($form_state->getValue($key))) > 0) && ($form_state->getValue($key) != $current_value)) {
$current_pass_failed = $form_state->isValueEmpty('current_pass') || !\Drupal::service('password')->check($form_state->getValue('current_pass'), $account);
if ($current_pass_failed) {
form_set_error('current_pass', $form_state, t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => $name)));
form_set_error($key, $form_state);
$form_state->setErrorByName('current_pass', t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => $name)));
$form_state->setErrorByName($key);
}
// We only need to check the password once.
break;
......
......@@ -165,7 +165,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
protected function valueValidate($form, FormStateInterface $form_state) {
if ($form_state->getValue(array('options', 'value')) == 'All' && !$form_state->isValueEmpty(array('options', 'expose', 'required'))) {
form_set_error('value', $form_state, t('You must select a value unless this is an non-required exposed filter.'));
$form_state->setErrorByName('value', t('You must select a value unless this is an non-required exposed filter.'));
}
}
......
......@@ -51,7 +51,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
);
}
else {
form_set_error('', $form_state, t('You have to add some fields to be able to use this filter.'));
$form_state->setErrorByName('', t('You have to add some fields to be able to use this filter.'));
}
}
}
......
......@@ -197,14 +197,14 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$error = TRUE;
}
if ($error) {
form_set_error('pager_options][expose][items_per_page_options', $form_state, t('Insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
$form_state->setErrorByName('pager_options][expose][items_per_page_options', t('Insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
}
// Make sure that the items_per_page is part of the expose settings.
if (!$form_state->isValueEmpty(array('pager_options', 'expose', 'items_per_page')) && !$form_state->isValueEmpty(array('pager_options', 'items_per_page'))) {
$items_per_page = $form_state->getValue(array('pager_options', 'items_per_page'));
if (array_search($items_per_page, $options) === FALSE) {
form_set_error('pager_options][expose][items_per_page_options', $form_state, t("The <em>Exposed items per page</em> field's options must include the value from the <em>Items per page</em> field (@items_per_page).",
$form_state->setErrorByName('pager_options][expose][items_per_page_options', t("The <em>Exposed items per page</em> field's options must include the value from the <em>Items per page</em> field (@items_per_page).",
array('@items_per_page' => $items_per_page))
);
}
......@@ -371,7 +371,7 @@ public function exposedFormAlter(&$form, FormStateInterface $form_state) {
public function exposedFormValidate(&$form, FormStateInterface $form_state) {
if (!$form_state->isValueEmpty('offset') && trim($form_state->getValue('offset'))) {
if (!is_numeric($form_state->getValue('offset')) || $form_state->getValue('offset') < 0) {
form_set_error('offset', $form_state, t('Offset must be an number greather or equal than 0.'));
$form_state->setErrorByName('offset', t('Offset must be an number greather or equal than 0.'));
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment