public function CustomStylesEditForm::validateForm in BeautyTips 8
Form validation handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::validateForm
File
- beautytips_manager/
src/ Form/ CustomStylesEditForm.php, line 109
Class
Namespace
Drupal\beautytips_manager\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if (empty($values['name'])) {
$form_state
->setErrorByName('name', t('You must name this custom style.'));
}
if (preg_match('/[^a-zA-Z0-9_]/', $values['name'])) {
$form_state
->setErrorByName('name', t('Style name must be alphanumeric or underscores only.'));
}
$integer_fields = [
'strokeWidth' => 'style',
'cornerRadius' => 'style',
'spikeGirth' => 'style',
'spikeLength' => 'style',
'shadowBlur' => 'style',
];
$pixel_fields = [
'width' => 'style',
'padding' => 'style',
'fontSize' => 'css',
];
// Validate fields that expect a number
foreach ($integer_fields as $name => $type) {
$value = $type == 'css' ? $values['custom_styles']['css-styles'][$name] : $values['custom_styles'][$name];
if ($value) {
if (!ctype_digit($value)) {
$error_element = $type == 'css' ? 'custom_styles][css-styles][' . $name : 'custom_styles][' . $name;
$form_state
->setErrorByName($error_element, t('You need to enter an integer value for <em>@name</em>', [
'@name' => $name,
]));
}
}
}
// Validate fields that expect a number and unit
foreach ($pixel_fields as $name => $type) {
$value = $type == 'css' ? $values['custom_styles']['css-styles'][$name] : $values['custom_styles'][$name];
if ($value) {
$unit = substr($value, -2, 2);
$value = str_replace([
'px',
' ',
'em',
], '', $value);
if (!is_numeric($value) || !$value && $value != 0 || !in_array($unit, [
'px',
'em',
])) {
$error_element = $type == 'css' ? 'custom_styles][css-styles][' . $name : 'custom_styles][' . $name;
$form_state
->setErrorByName($error_element, t('You need to enter a numeric value for <em>@name</em>, followed by <em>px</em> or <em>em</em>', [
'@name' => $name,
]));
}
}
}
}