Skip to content
Snippets Groups Projects
Commit 4dc3c3ec authored by Scott Euser's avatar Scott Euser
Browse files

Issue #2843009 by rene-bakx, scott_euser: Name validation of Template key fails

parent 0777cbf5
No related branches found
Tags 8.x-1.4
No related merge requests found
......@@ -67,31 +67,32 @@ class SiteSettingsConfigForm extends ConfigFormBase {
// Global setting.
$form['template_key'] = array(
'#type' => 'textfield',
'#type' => 'machine_name',
'#title' => t('Template key'),
'#description' => t('The key at which site settings should be made available in templates such as {{ site_settings.your_settings_group.your_setting_name }} with a template key of "site_settings".'),
'#default_value' => $config->get('template_key'),
'#required' => TRUE,
'#machine_name' => [
'exists' => [$this, 'machineNameExists'],
],
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
* Machine name validation callback.
*
* This method needs to exist, but there can be only one so it never exists.
*
* @param string $value
* The input value.
*
* @return bool
* That the machine name does not exist.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$template_key = $form_state->getValue('template_key');
// Allow only alphanumeric, underscores only as concatenator.
$regex = '/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/';
if ($template_key != preg_replace($regex, "", $template_key)) {
$error = $this->t('Please use only alphanumeric characters. You may concatenate words with underscores.');
$form_state->setErrorByName('template_key', $error);
}
parent::validateForm($form, $form_state);
public function machineNameExists($value) {
return FALSE;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment