Commit 560e5afd authored by alexpott's avatar alexpott

Issue #2145007 by tim.plunkett, h3rj4n: Convert form_set_error() in FormBase...

Issue #2145007 by tim.plunkett, h3rj4n: Convert form_set_error() in FormBase classes to use FormErrorInterface.
parent cdac25ee
......@@ -48,6 +48,13 @@ abstract class FormBase extends DependencySerialization implements FormInterface
*/
protected $configFactory;
/**
* The form error handler.
*
* @var \Drupal\Core\Form\FormErrorInterface
*/
protected $errorHandler;
/**
* {@inheritdoc}
*/
......@@ -214,4 +221,33 @@ protected function container() {
return \Drupal::getContainer();
}
/**
* Returns the form error handler.
*
* @return \Drupal\Core\Form\FormErrorInterface
* The form error handler.
*/
protected function errorHandler() {
if (!$this->errorHandler) {
$this->errorHandler = \Drupal::service('form_builder');
}
return $this->errorHandler;
}
/**
* Files an error against a form element.
*
* @param string $name
* The name of the form element.
* @param array $form_state
* An associative array containing the current state of the form.
* @param string $message
* (optional) The error message to present to the user.
*
* @see \Drupal\Core\Form\FormErrorInterface::setErrorByName()
*/
protected function setFormError($name, array &$form_state, $message = '') {
$this->errorHandler()->setErrorByName($name, $form_state, $message);
}
}
......@@ -92,15 +92,15 @@ interface FormErrorInterface {
* would be triggered if the input processing and validation steps were fully
* skipped.
*
* @param $name
* @param string $name
* The name of the form element. If the #parents property of your form
* element is array('foo', 'bar', 'baz') then you may set an error on 'foo'
* or 'foo][bar][baz'. Setting an error on 'foo' sets an error for every
* element where the #parents array starts with 'foo'.
* @param array $form_state
* An associative array containing the current state of the form.
* @param $message
* The error message to present to the user.
* @param string $message
* (optional) The error message to present to the user.
*
* @return mixed
* Return value is for internal use only. To get a list of errors, use
......
......@@ -70,10 +70,10 @@ public function validate(array $form, array &$form_state) {
$result = $feed_storage_controller->getFeedDuplicates($feed);
foreach ($result as $item) {
if (strcasecmp($item->title, $feed->label()) == 0) {
form_set_error('title', $form_state, $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));
$this->setFormError('title', $form_state, $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));
}
if (strcasecmp($item->url, $feed->url->value) == 0) {
form_set_error('url', $form_state, $this->t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $feed->url->value)));
$this->setFormError('url', $form_state, $this->t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $feed->url->value)));
}
}
parent::validate($form, $form_state);
......
......@@ -118,7 +118,7 @@ public function validateForm(array &$form, array &$form_state) {
// If both fields are empty or filled, cancel.
$file_upload = $this->getRequest()->files->get('files[upload]', NULL, TRUE);
if (empty($form_state['values']['remote']) == empty($file_upload)) {
form_set_error('remote', $form_state, $this->t('You must <em>either</em> upload a file or enter a URL.'));
$this->setFormError('remote', $form_state, $this->t('You must <em>either</em> upload a file or enter a URL.'));
}
}
......
......@@ -105,13 +105,13 @@ public function buildForm(array $form, array &$form_state, $default_ip = '') {
public function validateForm(array &$form, array &$form_state) {
$ip = trim($form_state['values']['ip']);
if ($this->ipManager->isBanned($ip)) {
form_set_error('ip', $form_state, $this->t('This IP address is already banned.'));
$this->setFormError('ip', $form_state, $this->t('This IP address is already banned.'));
}
elseif ($ip == $this->getRequest()->getClientIP()) {
form_set_error('ip', $form_state, $this->t('You may not ban your own IP address.'));
$this->setFormError('ip', $form_state, $this->t('You may not ban your own IP address.'));
}
elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
form_set_error('ip', $form_state, $this->t('Enter a valid IP address.'));
$this->setFormError('ip', $form_state, $this->t('Enter a valid IP address.'));
}
}
......
......@@ -237,7 +237,7 @@ public function validateForm(array &$form, array &$form_state) {
// @todo Inject this once https://drupal.org/node/2060865 is in.
$exists = \Drupal::entityManager()->getStorageController('custom_block')->loadByProperties(array('info' => $form_state['values']['info']));
if (!empty($exists)) {
form_set_error('info', $form_state, t('A block with description %name already exists.', array(
$this->setFormError('info', $form_state, $this->t('A block with description %name already exists.', array(
'%name' => $form_state['values']['info']
)));
}
......
......@@ -96,7 +96,7 @@ public function buildForm(array $form, array &$form_state, NodeInterface $node =
*/
public function validateForm(array &$form, array &$form_state) {
if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
form_set_error('', $form_state, $this->t('This book has been modified by another user, the changes could not be saved.'));
$this->setFormError('', $form_state, $this->t('This book has been modified by another user, the changes could not be saved.'));
}
}
......
......@@ -53,7 +53,7 @@ public function buildForm(array $form, array &$form_state) {
public function validateForm(array &$form, array &$form_state) {
$child_type = $form_state['values']['book_child_type'];
if (empty($form_state['values']['book_allowed_types'][$child_type])) {
form_set_error('book_child_type', $form_state, $this->t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => $this->t('Add child page'))));
$this->setFormError('book_child_type', $form_state, $this->t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => $this->t('Add child page'))));
}
parent::validateForm($form, $form_state);
......
......@@ -275,10 +275,10 @@ public function validate(array $form, array &$form_state) {
$date = $form_state['values']['date'];
if ($date instanceOf DrupalDateTime && $date->hasErrors()) {
form_set_error('date', $form_state, $this->t('You have to specify a valid date.'));
$this->setFormError('date', $form_state, $this->t('You have to specify a valid date.'));
}
if ($form_state['values']['name'] && !$form_state['values']['is_anonymous'] && !$account) {
form_set_error('name', $form_state, $this->t('You have to specify a valid author.'));
$this->setFormError('name', $form_state, $this->t('You have to specify a valid author.'));
}
}
elseif ($form_state['values']['is_anonymous']) {
......@@ -288,7 +288,7 @@ public function validate(array $form, array &$form_state) {
if ($form_state['values']['name']) {
$accounts = $this->entityManager->getStorageController('user')->loadByProperties(array('name' => $form_state['values']['name']));
if (!empty($accounts)) {
form_set_error('name', $form_state, $this->t('The name you used belongs to a registered user.'));
$this->setFormError('name', $form_state, $this->t('The name you used belongs to a registered user.'));
}
}
}
......
......@@ -79,7 +79,7 @@ public function validateForm(array &$form, array &$form_state) {
$form_state['values']['import_tarball'] = $file_upload->getRealPath();
}
else {
form_set_error('import_tarball', $form_state, $this->t('The import tarball could not be uploaded.'));
$this->setFormError('import_tarball', $form_state, $this->t('The import tarball could not be uploaded.'));
}
}
......@@ -100,7 +100,7 @@ public function submitForm(array &$form, array &$form_state) {
$form_state['redirect_route']['route_name'] = 'config.sync';
}
catch (\Exception $e) {
form_set_error('import_tarball', $form_state, $this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', array('@message' => $e->getMessage())));
$this->setFormError('import_tarball', $form_state, $this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', array('@message' => $e->getMessage())));
}
drupal_unlink($path);
}
......
......@@ -183,7 +183,7 @@ public function validateForm(array &$form, array &$form_state) {
$entity_storage = $this->entityManager->getStorageController($form_state['values']['config_type']);
// If an entity ID was not specified, set an error.
if (!isset($data[$id_key])) {
form_set_error('import', $form_state, $this->t('Missing ID key "@id_key" for this @entity_type import.', array('@id_key' => $id_key, '@entity_type' => $definition['label'])));
$this->setFormError('import', $form_state, $this->t('Missing ID key "@id_key" for this @entity_type import.', array('@id_key' => $id_key, '@entity_type' => $definition['label'])));
return;
}
$uuid_key = $definition['entity_keys']['uuid'];
......@@ -191,17 +191,17 @@ public function validateForm(array &$form, array &$form_state) {
if ($entity = $entity_storage->load($data[$id_key])) {
$this->configExists = $entity;
if (!isset($data[$uuid_key])) {
form_set_error('import', $form_state, $this->t('An entity with this machine name already exists but the import did not specify a UUID.'));
$this->setFormError('import', $form_state, $this->t('An entity with this machine name already exists but the import did not specify a UUID.'));
return;
}
if ($data[$uuid_key] !== $entity->uuid()) {
form_set_error('import', $form_state, $this->t('An entity with this machine name already exists but the UUID does not match.'));
$this->setFormError('import', $form_state, $this->t('An entity with this machine name already exists but the UUID does not match.'));
return;
}
}
// If there is no entity with a matching ID, check for a UUID match.
elseif (isset($data[$uuid_key]) && $entity_storage->loadByProperties(array($uuid_key => $data[$uuid_key]))) {
form_set_error('import', $form_state, $this->t('An entity with this UUID already exists but the machine name does not match.'));
$this->setFormError('import', $form_state, $this->t('An entity with this UUID already exists but the machine name does not match.'));
}
}
else {
......
......@@ -84,7 +84,7 @@ public function validate(array $form, array &$form_state) {
foreach ($recipients as &$recipient) {
$recipient = trim($recipient);
if (!valid_email_address($recipient)) {
form_set_error('recipients', $form_state, t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
$this->setFormError('recipients', $form_state, $this->t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
}
}
$form_state['values']['recipients'] = $recipients;
......
......@@ -7,19 +7,18 @@
namespace Drupal\edit\Form;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\user\TempStoreFactory;
use Drupal\Core\Entity\EntityChangedInterface;
/**
* Builds and process a form for editing a single entity field.
*/
class EditFieldForm implements FormInterface, ContainerInjectionInterface {
class EditFieldForm extends FormBase {
/**
* Stores the tempstore factory.
......@@ -145,7 +144,7 @@ public function validateForm(array &$form, array &$form_state) {
if ($changed_field_name = $this->getChangedFieldName($entity)) {
$changed_field_errors = $entity->$changed_field_name->validate();
if (count($changed_field_errors)) {
form_set_error('changed_field', $form_state, $changed_field_errors[0]->getMessage());
$this->setFormError('changed_field', $form_state, $changed_field_errors[0]->getMessage());
}
}
}
......
......@@ -315,12 +315,12 @@ protected function validateAddNew(array $form, array &$form_state) {
if (array_filter(array($field['label'], $field['field_name'], $field['type']))) {
// Missing label.
if (!$field['label']) {
form_set_error('fields][_add_new_field][label', $form_state, $this->t('Add new field: you need to provide a label.'));
$this->setFormError('fields][_add_new_field][label', $form_state, $this->t('Add new field: you need to provide a label.'));
}
// Missing field name.
if (!$field['field_name']) {
form_set_error('fields][_add_new_field][field_name', $form_state, $this->t('Add new field: you need to provide a field name.'));
$this->setFormError('fields][_add_new_field][field_name', $form_state, $this->t('Add new field: you need to provide a field name.'));
}
// Field name validation.
else {
......@@ -333,7 +333,7 @@ protected function validateAddNew(array $form, array &$form_state) {
// Missing field type.
if (!$field['type']) {
form_set_error('fields][_add_new_field][type', $form_state, $this->t('Add new field: you need to select a field type.'));
$this->setFormError('fields][_add_new_field][type', $form_state, $this->t('Add new field: you need to select a field type.'));
}
}
}
......@@ -359,12 +359,12 @@ protected function validateAddExisting(array $form, array &$form_state) {
if (array_filter(array($field['label'], $field['field_name']))) {
// Missing label.
if (!$field['label']) {
form_set_error('fields][_add_existing_field][label', $form_state, $this->t('Re-use existing field: you need to provide a label.'));
$this->setFormError('fields][_add_existing_field][label', $form_state, $this->t('Re-use existing field: you need to provide a label.'));
}
// Missing existing field name.
if (!$field['field_name']) {
form_set_error('fields][_add_existing_field][field_name', $form_state, $this->t('Re-use existing field: you need to select a field.'));
$this->setFormError('fields][_add_existing_field][field_name', $form_state, $this->t('Re-use existing field: you need to select a field.'));
}
}
}
......
......@@ -172,7 +172,7 @@ public function validateForm(array &$form, array &$form_state) {
$cardinality = $form_state['values']['field']['cardinality'];
$cardinality_number = $form_state['values']['field']['cardinality_number'];
if ($cardinality === 'number' && empty($cardinality_number)) {
form_error($form['field']['cardinality_container']['cardinality_number'], $form_state, $this->t('Number of values is required.'));
$this->setFormError('field][cardinality_number', $form_state, $this->t('Number of values is required.'));
}
}
......
......@@ -232,7 +232,7 @@ public function validate(array $form, array &$form_state) {
->condition('name', $format_name)
->execute();
if ($format_exists) {
form_set_error('name', $form_state, t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
$this->setFormError('name', $form_state, $this->t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
}
}
......
......@@ -156,7 +156,7 @@ public function form(array $form, array &$form_state) {
*/
public function effectValidate($form, &$form_state) {
if (!$form_state['values']['new']) {
form_error($form['effects']['new']['new'], $form_state, $this->t('Select an effect to add.'));
$this->setFormError('new', $form_state, $this->t('Select an effect to add.'));
}
}
......
......@@ -124,11 +124,11 @@ public function validateCustom(array $form, array &$form_state) {
$this->validateCommon($form['custom_language'], $form_state);
if ($language = language_load($langcode)) {
form_error($form['custom_language']['langcode'], $form_state, $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
$this->setFormError('langcode', $form_state, $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
}
}
else {
form_error($form['predefined_langcode'], $form_state, $this->t('Use the <em>Add language</em> button to save a predefined language.'));
$this->setFormError('predefined_langcode', $form_state, $this->t('Use the <em>Add language</em> button to save a predefined language.'));
}
}
......@@ -138,11 +138,11 @@ public function validateCustom(array $form, array &$form_state) {
public function validatePredefined($form, &$form_state) {
$langcode = $form_state['values']['predefined_langcode'];
if ($langcode == 'custom') {
form_error($form['predefined_langcode'], $form_state, $this->t('Fill in the language details and save the language with <em>Add custom language</em>.'));
$this->setFormError('predefined_langcode', $form_state, $this->t('Fill in the language details and save the language with <em>Add custom language</em>.'));
}
else {
if ($language = language_load($langcode)) {
form_error($form['predefined_langcode'], $form_state, $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
$this->setFormError('predefined_langcode', $form_state, $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
}
}
}
......
......@@ -70,10 +70,10 @@ public function commonForm(array &$form) {
public function validateCommon(array $form, array &$form_state) {
// Ensure sane field values for langcode and name.
if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state['values']['langcode'])) {
form_error($form['langcode'], $form_state, $this->t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
$this->setFormError('langcode', $form_state, $this->t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
}
if ($form_state['values']['name'] != check_plain($form_state['values']['name'])) {
form_error($form['name'], $form_state, $this->t('%field cannot contain any markup.', array('%field' => $form['name']['#title'])));
$this->setFormError('name', $form_state, $this->t('%field cannot contain any markup.', array('%field' => $form['name']['#title'])));
}
}
......
......@@ -143,10 +143,10 @@ public function validateForm(array &$form, array &$form_state) {
foreach ($mappings as $key => $data) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
form_set_error('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes must be unique.'));
$this->setFormError('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
form_set_error('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
$this->setFormError('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
......@@ -157,10 +157,10 @@ public function validateForm(array &$form, array &$form_state) {
if (!empty($data['browser_langcode'])) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
form_set_error('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes must be unique.'));
$this->setFormError('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
form_set_error('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
$this->setFormError('mappings][' . $key . '][browser_langcode', $form_state, $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
......
......@@ -106,18 +106,18 @@ public function validateForm(array &$form, array &$form_state) {
if (!$language->default && $form_state['values']['language_negotiation_url_part'] == LANGUAGE_NEGOTIATION_URL_PREFIX) {
// Throw a form error if the prefix is blank for a non-default language,
// although it is required for selected negotiation type.
form_error($form['prefix'][$langcode], $form_state, t('The prefix may only be left blank for the default language.'));
$this->setFormError("prefix][$langcode", $form_state, t('The prefix may only be left blank for the default language.'));
}
}
elseif (strpos($value, '/') !== FALSE) {
// Throw a form error if the string contains a slash,
// which would not work.
form_error($form['prefix'][$langcode], $form_state, t('The prefix may not contain a slash.'));
$this->setFormError("prefix][$langcode", $form_state, t('The prefix may not contain a slash.'));
}
elseif (isset($count[$value]) && $count[$value] > 1) {
// Throw a form error if there are two languages with the same
// domain/prefix.
form_error($form['prefix'][$langcode], $form_state, t('The prefix for %language, %value, is not unique.', array('%language' => $language->name, '%value' => $value)));
$this->setFormError("prefix][$langcode", $form_state, t('The prefix for %language, %value, is not unique.', array('%language' => $language->name, '%value' => $value)));
}
}
......@@ -130,13 +130,13 @@ public function validateForm(array &$form, array &$form_state) {
if (!$language->default && $form_state['values']['language_negotiation_url_part'] == LANGUAGE_NEGOTIATION_URL_DOMAIN) {
// Throw a form error if the domain is blank for a non-default language,
// although it is required for selected negotiation type.
form_error($form['domain'][$langcode], $form_state, t('The domain may only be left blank for the default language.'));
$this->setFormError("domain][$langcode", $form_state, t('The domain may only be left blank for the default language.'));
}
}
elseif (isset($count[$value]) && $count[$value] > 1) {
// Throw a form error if there are two languages with the same
// domain/domain.
form_error($form['domain'][$langcode], $form_state, t('The domain for %language, %value, is not unique.', array('%language' => $language->name, '%value' => $value)));
$this->setFormError("domain][$langcode", $form_state, t('The domain for %language, %value, is not unique.', array('%language' => $language->name, '%value' => $value)));
}
}
......@@ -147,7 +147,7 @@ public function validateForm(array &$form, array &$form_state) {
// Ensure we have exactly one protocol when checking the hostname.
$host = 'http://' . str_replace(array('http://', 'https://'), '', $value);
if (parse_url($host, PHP_URL_HOST) != $value) {
form_error($form['domain'][$langcode], $form_state, t('The domain for %language may only contain the domain name, not a protocol and/or port.', array('%language' => $name)));
$this->setFormError("domain][$langcode", $form_state, t('The domain for %language may only contain the domain name, not a protocol and/or port.', array('%language' => $name)));
}
}
}
......
......@@ -93,7 +93,7 @@ public function validateForm(array &$form, array &$form_state) {
parent::validateForm($form, $form_state);
if (empty($form['#translation_directory']) && $form_state['values']['use_source'] == LOCALE_TRANSLATION_USE_SOURCE_LOCAL) {
form_set_error('use_source', $form_state, t('You have selected local translation source, but no <a href="@url">Interface translation directory</a> was configured.', array('@url' => url('admin/config/media/file-system'))));
$this->setFormError('use_source', $form_state, $this->t('You have selected local translation source, but no <a href="@url">Interface translation directory</a> was configured.', array('@url' => url('admin/config/media/file-system'))));
}
}
......
......@@ -166,8 +166,8 @@ public function validateForm(array &$form, array &$form_state) {
foreach ($form_state['values']['strings'] as $lid => $translations) {
foreach ($translations['translations'] as $key => $value) {
if (!locale_string_is_safe($value)) {
form_set_error("strings][$lid][translations][$key", $form_state, $this->t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
form_set_error("translations][$langcode][$key", $form_state, $this->t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
$this->setFormError("strings][$lid][translations][$key", $form_state, $this->t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
$this->setFormError("translations][$langcode][$key", $form_state, $this->t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
watchdog('locale', 'Attempted submission of a translation string with disallowed HTML: %string', array('%string' => $value), WATCHDOG_WARNING);
}
}
......
......@@ -234,7 +234,7 @@ public function validate(array $form, array &$form_state) {
}
}
if (!trim($menu_link->link_path) || !drupal_valid_path($menu_link->link_path, TRUE)) {
form_set_error('link_path', $form_state, t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $menu_link->link_path)));
$this->setFormError('link_path', $form_state, $this->t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $menu_link->link_path)));
}
parent::validate($form, $form_state);
......
......@@ -318,7 +318,7 @@ public function validate(array $form, array &$form_state) {
$node = $this->buildEntity($form, $form_state);
if ($node->id() && (node_last_changed($node->id(), $this->getFormLangcode($form_state)) > $node->getChangedTime())) {
form_set_error('changed', $form_state, t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
$this->setFormError('changed', $form_state, $this->t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
}
// Validate the "authored by" field.
......@@ -326,14 +326,14 @@ public function validate(array $form, array &$form_state) {
// The use of empty() is mandatory in the context of usernames
// as the empty string denotes the anonymous user. In case we
// are dealing with an anonymous user we set the user ID to 0.
form_set_error('name', $form_state, t('The username %name does not exist.', array('%name' => $form_state['values']['name'])));
$this->setFormError('name', $form_state, $this->t('The username %name does not exist.', array('%name' => $form_state['values']['name'])));
}
// Validate the "authored on" field.
// The date element contains the date object.
$date = $node->date instanceof DrupalDateTime ? $node->date : new DrupalDateTime($node->date);
if ($date->hasErrors()) {
form_set_error('date', $form_state, t('You have to specify a valid date.'));
$this->setFormError('date', $form_state, $this->t('You have to specify a valid date.'));
}
// Invoke hook_node_validate() for validation needed by modules.
......
......@@ -182,7 +182,7 @@ public function validate(array $form, array &$form_state) {
$id = trim($form_state['values']['type']);
// '0' is invalid, since elsewhere we check it using empty().
if ($id == '0') {
form_set_error('type', $form_state, t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $id)));
$this->setFormError('type', $form_state, $this->t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $id)));
}
}
......
......@@ -126,7 +126,7 @@ public function validate(array $form, array &$form_state) {
}
// Make sure at least one mapping is defined.
elseif (!$picture_mapping->isNew() && !$picture_mapping->hasMappings()) {
form_set_error('mappings', $form_state, $this->t('Please select at least one mapping.'));
$this->setFormError('mappings', $form_state, $this->t('Please select at least one mapping.'));
}
}
}
......
......@@ -58,7 +58,7 @@ public function submitForm(array &$form, array &$form_state) {
// "field is required" because the search keywords field has no title.
// The error message would also complain about a missing #title field.)
if ($form_state['values']['search_block_form'] == '') {
form_set_error('keys', $form_state, $this->t('Please enter some keywords.'));
$this->setFormError('keys', $form_state, $this->t('Please enter some keywords.'));
}
$form_id = $form['form_id']['#value'];
......@@ -72,7 +72,7 @@ public function submitForm(array &$form, array &$form_state) {
);
}
else {
form_set_error(NULL, $form_state, $this->t('Search is currently disabled.'), 'error');
$this->setFormError('', $form_state, $this->t('Search is currently disabled.'), 'error');
}
}
}
......@@ -112,7 +112,7 @@ public function validateForm(array &$form, array &$form_state) {
public function submitForm(array &$form, array &$form_state) {
$keys = $form_state['values']['processed_keys'];
if ($keys == '') {
form_set_error('keys', $form_state, t('Please enter some keywords.'));
$this->setFormError('keys', $form_state, $this->t('Please enter some keywords.'));
// Fall through to the form redirect.
}
......
......@@ -219,7 +219,7 @@ public function validateForm(array &$form, array &$form_state) {
$new_plugins = array_filter($form_state['values']['active_plugins']);
$default = $form_state['values']['default_plugin'];
if (!in_array($default, $new_plugins, TRUE)) {
form_set_error('default_plugin', $form_state, $this->t('Your default search plugin is not selected as an active plugin.'));
$this->setFormError('default_plugin', $form_state, $this->t('Your default search plugin is not selected as an active plugin.'));
}
}
// Handle per-plugin validation logic.
......
......@@ -65,7 +65,7 @@ public function validate(array $form, array &$form_state) {
$entity = $this->entity;
// Check to prevent a duplicate title.
if ($form_state['values']['label'] != $entity->label() && shortcut_set_title_exists($form_state['values']['label'])) {
form_set_error('label', $form_state, t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['label'])));
$this->setFormError('label', $form_state, $this->t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['label'])));
}
}
......
......@@ -97,7 +97,7 @@ public function validateForm(array &$form, array &$form_state) {
// If a password was provided but a username wasn't, the credentials are
// incorrect, so throw an error.
if (empty($form_state['values']['simpletest_httpauth_username']) && !empty($form_state['values']['simpletest_httpauth_password'])) {
form_set_error('simpletest_httpauth_username', $form_state, $this->t('HTTP authentication credentials must include a username in addition to a password.'));
$this->setFormError('simpletest_httpauth_username', $form_state, $this->t('HTTP authentication credentials must include a username in addition to a password.'));
}
parent::validateForm($form, $form_state);
......
......@@ -191,7 +191,7 @@ public function validate(array $form, array &$form_state) {
$pattern = trim($form_state['values']['date_format_pattern']);
foreach ($this->dateFormatStorage->loadMultiple() as $format) {
if ($format->getPattern() == $pattern && ($this->entity->isNew() || $format->id() != $this->entity->id())) {
form_set_error('date_format_pattern', $form_state, t('This format already exists. Enter a unique format string.'));
$this->setFormError('date_format_pattern', $form_state, $this->t('This format already exists. Enter a unique format string.'));
continue;
}
}
......
......@@ -144,7 +144,7 @@ public function validateForm(array &$form, array &$form_state) {
}
// Validate front page path.
if (!drupal_valid_path($form_state['values']['site_frontpage'])) {
form_set_error('site_frontpage', $form_state, t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_frontpage'])));
$this->setFormError('site_frontpage', $form_state, $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_frontpage'])));
}
// Get the normal paths of both error pages.
if (!empty($form_state['values']['site_403'])) {
......@@ -155,11 +155,11 @@ public function validateForm(array &$form, array &$form_state) {
}
// Validate 403 error path.
if (!empty($form_state['values']['site_403']) && !drupal_valid_path($form_state['values']['site_403'])) {
form_set_error('site_403', $form_state, t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_403'])));
$this->setFormError('site_403', $form_state, $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_403'])));
}
// Validate 404 error path.
if (!empty($form_state['values']['site_404']) && !drupal_valid_path($form_state['values']['site_404'])) {
form_set_error('site_404', $form_state, t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_404'])));
$this->setFormError('site_404', $form_state, $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_404'])));
}
parent::validateForm($form, $form_state);
......
......@@ -332,7 +332,7 @@ public function validateForm(array &$form, array &$form_state) {
}
else {
// File upload failed.
form_set_error('logo_upload', $form_state, t('The logo could not be uploaded.'));
$this->setFormError('logo_upload', $form_state, $this->t('The logo could not be uploaded.'));
}
}
......@@ -348,7 +348,7 @@ public function validateForm(array &$form, array &$form_state) {
}
else {
// File upload failed.
form_set_error('favicon_upload', $form_state, t('The favicon could not be uploaded.'));
$this->setFormError('favicon_upload', $form_state, $this->t('The favicon could not be uploaded.'));
}
}
......@@ -357,13 +357,13 @@ public function validateForm(array &$form, array &$form_state) {
if ($form_state['values']['logo_path']) {
$path = $this->validatePath($form_state['values']['logo_path']);
if (!$path) {
form_set_error('logo_path', $form_state, t('The custom logo path is invalid.'));
$this->setFormError('logo_path', $form_state, $this->t('The custom logo path is invalid.'));
}
}
if ($form_state['values']['favicon_path']) {
$path = $this->validatePath($form_state['values']['favicon_path']);
if (!$path) {
form_set_error('favicon_path', $form_state, t('The custom favicon path is invalid.'));
$this->setFormError('favicon_path', $form_state, $this->t('The custom favicon path is invalid.'));
}
}
}
......