Commit d39c0147 authored by alexpott's avatar alexpott

Issue #2334383 by jbrown, ashutoshsngh, er.pushpinderrana: Remove usage of form_error().

parent d4812031
......@@ -84,18 +84,6 @@ function form_execute_handlers($type, &$form, FormStateInterface $form_state) {
}
}
/**
* Flags an element as having an error.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use $form_state->setError().
*
* @see \Drupal\Core\Form\FormStateInterface::setError().
*/
function form_error(&$element, FormStateInterface $form_state, $message = '') {
$form_state->setError($element, $message);
}
/**
* Builds and processes all elements in the structured form array.
*
......
......@@ -193,7 +193,7 @@ public static function processMachineName(&$element, FormStateInterface $form_st
public static function validateMachineName(&$element, FormStateInterface $form_state, &$complete_form) {
// Verify that the machine name not only consists of replacement tokens.
if (preg_match('@^' . $element['#machine_name']['replace'] . '+$@', $element['#value'])) {
form_error($element, $form_state, t('The machine-readable name must contain unique characters.'));
$form_state->setError($element, t('The machine-readable name must contain unique characters.'));
}
// Verify that the machine name contains no disallowed characters.
......@@ -202,15 +202,15 @@ public static function validateMachineName(&$element, FormStateInterface $form_s
// Since a hyphen is the most common alternative replacement character,
// a corresponding validation error message is supported here.
if ($element['#machine_name']['replace'] == '-') {
form_error($element, $form_state, t('The machine-readable name must contain only lowercase letters, numbers, and hyphens.'));
$form_state->setError($element, t('The machine-readable name must contain only lowercase letters, numbers, and hyphens.'));
}
// Otherwise, we assume the default (underscore).
else {
form_error($element, $form_state, t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
$form_state->setError($element, t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
}
}
else {
form_error($element, $form_state, $element['#machine_name']['error']);
$form_state->setError($element, $element['#machine_name']['error']);
}
}
......@@ -218,7 +218,7 @@ public static function validateMachineName(&$element, FormStateInterface $form_s
if ($element['#default_value'] !== $element['#value']) {
$function = $element['#machine_name']['exists'];
if (call_user_func($function, $element['#value'], $element, $form_state)) {
form_error($element, $form_state, t('The machine-readable name is already in use. It must be unique.'));
$form_state->setError($element, t('The machine-readable name is already in use. It must be unique.'));
}
}
}
......
......@@ -97,7 +97,7 @@ public function settingsForm(array $form, FormStateInterface $form_state, Editor
*/
public function validateStylesValue(array $element, FormStateInterface $form_state) {
if ($this->generateStylesSetSetting($element['#value']) === FALSE) {
form_error($element, $form_state, t('The provided list of styles is syntactically incorrect.'));
$form_state->setError($element, t('The provided list of styles is syntactically incorrect.'));
}
}
......
......@@ -57,7 +57,7 @@ public function settingsForm(array $form, FormStateInterface $form_state, Editor
* Validates the settings form for an editor.
*
* The contents of the editor settings are located in
* $form_state->getValue(array('editor', 'settings')). Calls to form_error()
* $form_state->getValue(array('editor', 'settings')). Calls to $form_state->setError()
* should reflect this location in the settings form.
*
* @param array $form
......
......@@ -243,13 +243,13 @@ public function validateAutocompleteInput($input, &$element, FormStateInterface
if (empty($entities)) {
if ($strict) {
// Error if there are no entities available for a required field.
form_error($element, $form_state, t('There are no entities matching "%value".', $params));
$form_state->setError($element, t('There are no entities matching "%value".', $params));
}
}
elseif (count($entities) > 5) {
$params['@id'] = key($entities);
// Error if there are more than 5 matching entities.
form_error($element, $form_state, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params));
$form_state->setError($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params));
}
elseif (count($entities) > 1) {
// More helpful error if there are only a few matching entities.
......@@ -258,7 +258,7 @@ public function validateAutocompleteInput($input, &$element, FormStateInterface
$multiples[] = $name . ' (' . $id . ')';
}
$params['@id'] = $id;
form_error($element, $form_state, t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples))));
$form_state->setError($element, t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples))));
}
else {
// Take the one and only matching entity.
......
......@@ -1339,19 +1339,19 @@ function file_managed_file_validate(&$element, FormStateInterface $form_state) {
if ($file->isPermanent()) {
$references = \Drupal::service('file.usage')->listUsage($file);
if (empty($references)) {
form_error($element, $form_state, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
$form_state->setError($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
}
}
}
else {
form_error($element, $form_state, t('The file referenced by the !name field does not exist.', array('!name' => $element['#title'])));
$form_state->setError($element, t('The file referenced by the !name field does not exist.', array('!name' => $element['#title'])));
}
}
}
// Check required property based on the FID.
if ($element['#required'] && empty($element['fids']['#value']) && !in_array($clicked_button, array('upload_button', 'remove_button'))) {
form_error($element['upload'], $form_state, t('!name field is required.', array('!name' => $element['#title'])));
$form_state->setError($element['upload'], t('!name field is required.', array('!name' => $element['#title'])));
}
// Consolidate the array value of this field to array of FIDs.
......
......@@ -233,7 +233,7 @@ public static function validateExtensions($element, FormStateInterface $form_sta
$extensions = array_filter(explode(' ', $extensions));
$extensions = implode(' ', array_unique($extensions));
if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
form_error($element, $form_state, t('The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.'));
$form_state->setError($element, t('The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.'));
}
else {
form_set_value($element, $extensions, $form_state);
......@@ -252,7 +252,7 @@ public static function validateExtensions($element, FormStateInterface $form_sta
*/
public static function validateMaxFilesize($element, FormStateInterface $form_state) {
if (!empty($element['#value']) && !is_numeric(Bytes::toInt($element['#value']))) {
form_error($element, $form_state, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('!name' => t($element['title']))));
$form_state->setError($element, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('!name' => t($element['title']))));
}
}
......
......@@ -371,7 +371,7 @@ public static function validateResolution($element, FormStateInterface $form_sta
if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) {
foreach (array('x', 'y') as $dimension) {
if (!$element[$dimension]['#value']) {
form_error($element[$dimension], $form_state, t('Both a height and width value must be specified in the !name field.', array('!name' => $element['#title'])));
$form_state->setError($element[$dimension], t('Both a height and width value must be specified in the !name field.', array('!name' => $element['#title'])));
return;
}
}
......
......@@ -71,7 +71,6 @@ protected function defineOptions() {
return $options;
}
/**
* Overrides \Drupal\views\Plugin\views\row\RowPluginBase::buildOptionsForm().
*/
......@@ -114,7 +113,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
*/
public function validateAliasName($element, FormStateInterface $form_state) {
if (preg_match('@[^A-Za-z0-9_-]+@', $element['#value'])) {
form_error($element, $form_state, t('The machine-readable name must contain only letters, numbers, dashes and underscores.'));
$form_state->setError($element, t('The machine-readable name must contain only letters, numbers, dashes and underscores.'));
}
}
......
......@@ -90,7 +90,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
*/
public function elementValidateLimitValidationErrors($element, FormStateInterface $form_state) {
if ($element['#value'] == 'invalid') {
form_error($element, $form_state, t('@label element is invalid', array('@label' => $element['#title'])));
$form_state->setError($element, t('@label element is invalid', array('@label' => $element['#title'])));
}
}
......
......@@ -79,7 +79,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
public function elementValidateRequired($element, FormStateInterface $form_state) {
// Set a custom validation error on the #required element.
if (!empty($element['#required_but_empty']) && isset($element['#form_test_required_error'])) {
form_error($element, $form_state, $element['#form_test_required_error']);
$form_state->setError($element, $element['#form_test_required_error']);
}
}
......
......@@ -322,7 +322,7 @@ function validate_term_strings(&$form, $values, FormStateInterface $form_state)
}
if ($missing && !empty($this->options['error_message'])) {
form_error($form, $form_state, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
$form_state->setError($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
}
elseif ($missing && empty($this->options['error_message'])) {
$tids = array(0);
......
......@@ -86,7 +86,7 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$role = array_filter($role);
if (!$role) {
form_error($form['role'], $form_state, t('You must select at least one role if type is "by role"'));
$form_state->setError($form['role'], t('You must select at least one role if type is "by role"'));
}
$form_state->setValue(array('access_options', 'role'), $role);
......
......@@ -135,7 +135,7 @@ function validate_user_strings(&$form, FormStateInterface $form_state, $values)
}
if ($missing) {
form_error($form, $form_state, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', array_keys($missing)))));
$form_state->setError($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', array_keys($missing)))));
}
return $uids;
......
......@@ -222,7 +222,7 @@ public function settingsFormValidate($element, FormStateInterface $form_state, $
list($view, $display) = explode(':', $element['view_and_display']['#value']);
}
else {
form_error($element, $form_state, t('The views entity selection mode requires a view.'));
$form_state->setError($element, t('The views entity selection mode requires a view.'));
return;
}
......
......@@ -129,7 +129,7 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
foreach ($custom_fields as $field) {
$cache_options = $form_state->getValue('cache_options');
if ($cache_options[$field] == 'custom' && !is_numeric($cache_options[$field . '_custom'])) {
form_error($form[$field .'_custom'], $form_state, t('Custom time values must be numeric.'));
$form_state->setError($form[$field .'_custom'], t('Custom time values must be numeric.'));
}
}
}
......
......@@ -1871,24 +1871,24 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
switch ($section) {
case 'display_title':
if ($form_state->isValueEmpty('display_title')) {
form_error($form['display_title'], $form_state, t('Display title may not be empty.'));
$form_state->setError($form['display_title'], t('Display title may not be empty.'));
}
break;
case 'css_class':
$css_class = $form_state->getValue('css_class');
if (preg_match('/[^a-zA-Z0-9-_ ]/', $css_class)) {
form_error($form['css_class'], $form_state, t('CSS classes must be alphanumeric or dashes only.'));
$form_state->setError($form['css_class'], t('CSS classes must be alphanumeric or dashes only.'));
}
break;
case 'display_id':
if ($form_state->getValue('display_id')) {
if (preg_match('/[^a-z0-9_]/', $form_state->getValue('display_id'))) {
form_error($form['display_id'], $form_state, t('Display name must be letters, numbers, or underscores only.'));
$form_state->setError($form['display_id'], t('Display name must be letters, numbers, or underscores only.'));
}
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_error($form['display_id'], $form_state, t('Display id should be unique.'));
$form_state->setError($form['display_id'], t('Display id should be unique.'));
}
}
}
......
......@@ -387,19 +387,19 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$path = $this->getOption('path');
$menu_type = $form_state->getValue(array('menu', 'type'));
if ($menu_type == 'normal' && strpos($path, '%') !== FALSE) {
form_error($form['menu']['type'], $form_state, t('Views cannot create normal menu items for paths with a % in them.'));
$form_state->setError($form['menu']['type'], t('Views cannot create normal menu items for paths with a % in them.'));
}
if ($menu_type == 'default tab' || $menu_type == 'tab') {
$bits = explode('/', $path);
$last = array_pop($bits);
if ($last == '%') {
form_error($form['menu']['type'], $form_state, t('A display whose path ends with a % cannot be a tab.'));
$form_state->setError($form['menu']['type'], t('A display whose path ends with a % cannot be a tab.'));
}
}
if ($menu_type != 'none' && $form_state->isValueEmpty(array('menu', 'title'))) {
form_error($form['menu']['title'], $form_state, t('Title is required for this menu type.'));
$form_state->setError($form['menu']['title'], t('Title is required for this menu type.'));
}
}
}
......
......@@ -57,7 +57,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
// Require a key if the format is key.
if ($form_state->getValue(array('options', 'format')) == 'key' && $form_state->getValue(array('options', 'key')) == '') {
form_error($form['key'], $form_state, t('You have to enter a key if you want to display a key of the data.'));
$form_state->setError($form['key'], t('You have to enter a key if you want to display a key of the data.'));
}
}
......
......@@ -87,17 +87,17 @@ public function validateValidTime(&$form, FormStateInterface $form_state, $opera
if ($operators[$operator]['values'] == 1) {
$convert = strtotime($value['value']);
if (!empty($form['value']) && ($convert == -1 || $convert === FALSE)) {
form_error($form['value'], $form_state, t('Invalid date format.'));
$form_state->setError($form['value'], t('Invalid date format.'));
}
}
elseif ($operators[$operator]['values'] == 2) {
$min = strtotime($value['min']);
if ($min == -1 || $min === FALSE) {
form_error($form['min'], $form_state, t('Invalid date format.'));
$form_state->setError($form['min'], t('Invalid date format.'));
}
$max = strtotime($value['max']);
if ($max == -1 || $max === FALSE) {
form_error($form['max'], $form_state, t('Invalid date format.'));
$form_state->setError($form['max'], t('Invalid date format.'));
}
}
}
......@@ -115,14 +115,14 @@ protected function buildGroupValidate($form, FormStateInterface $form_state) {
// Check if the title is defined but value wasn't defined.
if (!empty($group['title'])) {
if ((!is_array($group['value']) && empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) == 1)) {
form_error($form['group_info']['group_items'][$id]['value'], $form_state, t('The value is required if title for this item is defined.'));
$form_state->setError($form['group_info']['group_items'][$id]['value'], t('The value is required if title for this item is defined.'));
}
}
// Check if the value is defined but title wasn't defined.
if ((!is_array($group['value']) && !empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) > 1)) {
if (empty($group['title'])) {
form_error($form['group_info']['group_items'][$id]['title'], $form_state, t('The title is required if value for this item is defined.'));
$form_state->setError($form['group_info']['group_items'][$id]['title'], t('The title is required if value for this item is defined.'));
}
}
}
......
......@@ -620,14 +620,14 @@ public function buildExposeForm(&$form, FormStateInterface $form_state) {
public function validateExposeForm($form, FormStateInterface $form_state) {
$identifier = $form_state->getValue(array('options', 'expose', 'identifier'));
if (empty($identifier)) {
form_error($form['expose']['identifier'], $form_state, t('The identifier is required if the filter is exposed.'));
$form_state->setError($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
}
elseif ($identifier == 'value') {
form_error($form['expose']['identifier'], $form_state, t('This identifier is not allowed.'));
$form_state->setError($form['expose']['identifier'], t('This identifier is not allowed.'));
}
if (!$this->view->display_handler->isIdentifierUnique($form_state->get('id'), $identifier)) {
form_error($form['expose']['identifier'], $form_state, t('This identifier is used by another handler.'));
$form_state->setError($form['expose']['identifier'], t('This identifier is used by another handler.'));
}
}
......@@ -638,15 +638,15 @@ protected function buildGroupValidate($form, FormStateInterface $form_state) {
if (!$form_state->isValueEmpty(array('options', 'group_info'))) {
$identifier = $form_state->getValue(array('options', 'group_info', 'identifier'));
if (empty($identifier)) {
form_error($form['group_info']['identifier'], $form_state, t('The identifier is required if the filter is exposed.'));
$form_state->setError($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
}
elseif ($identifier == 'value') {
form_error($form['group_info']['identifier'], $form_state, t('This identifier is not allowed.'));
$form_state->setError($form['group_info']['identifier'], t('This identifier is not allowed.'));
}
if (!$this->view->display_handler->isIdentifierUnique($form_state->get('id'), $identifier)) {
form_error($form['group_info']['identifier'], $form_state, t('This identifier is used by another handler.'));
$form_state->setError($form['group_info']['identifier'], t('This identifier is used by another handler.'));
}
}
......@@ -660,8 +660,7 @@ protected function buildGroupValidate($form, FormStateInterface $form_state) {
if (!empty($group['title']) && $operators[$group['operator']]['values'] > 0) {
if ((!is_array($group['value']) && trim($group['value']) == "") ||
(is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) == 0)) {
form_error($form['group_info']['group_items'][$id]['value'], $form_state,
t('The value is required if title for this item is defined.'));
$form_state->setError($form['group_info']['group_items'][$id]['value'], t('The value is required if title for this item is defined.'));
}
}
......@@ -669,8 +668,7 @@ protected function buildGroupValidate($form, FormStateInterface $form_state) {
if ((!is_array($group['value']) && trim($group['value']) != "") ||
(is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) > 0)) {
if (empty($group['title'])) {
form_error($form['group_info']['group_items'][$id]['title'], $form_state,
t('The title is required if value for this item is defined.'));
$form_state->setError($form['group_info']['group_items'][$id]['title'], t('The title is required if value for this item is defined.'));
}
}
}
......
......@@ -100,7 +100,7 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
switch ($form_state->get('section')) {
case 'test_option':
if (!trim($form_state->getValue('test_option'))) {
form_error($form['test_option'], $form_state, t('You cannot have an empty option.'));
$form_state->setError($form['test_option'], t('You cannot have an empty option.'));
}
break;
}
......
......@@ -939,7 +939,7 @@ function views_element_validate_tags($element, FormStateInterface $form_state) {
$values = array_map('trim', explode(',', $element['#value']));
foreach ($values as $value) {
if (preg_match("/[^a-z_]/", $value)) {
form_error($element, $form_state, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
$form_state->setError($element, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
return;
}
}
......
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