protected function Required::getRules in Clientside Validation 3.0.x
Same name and namespace in other branches
- 8.2 src/Plugin/CvValidator/Required.php \Drupal\clientside_validation\Plugin\CvValidator\Required::getRules()
- 8 src/Plugin/CvValidator/Required.php \Drupal\clientside_validation\Plugin\CvValidator\Required::getRules()
- 2.0.x src/Plugin/CvValidator/Required.php \Drupal\clientside_validation\Plugin\CvValidator\Required::getRules()
Get the validation rules for this form element.
Return value
array An array with following keys:
- rules: An array with the rulename as key and the rule arguments as value.
- messages: An array with the rulename as key and the message for this rule as argument.
Overrides CvValidatorBase::getRules
File
- src/
Plugin/ CvValidator/ Required.php, line 36
Class
- Required
- Provides a 'required' validator.
Namespace
Drupal\clientside_validation\Plugin\CvValidatorCode
protected function getRules($element, FormStateInterface $form_state) {
$is_required = $this
->getAttributeValue($element, 'required');
$states = $this
->getAttributeValue($element, 'states') ?: [];
$is_conditionally_required = FALSE;
if (is_array($states) && !empty($states)) {
$is_conditionally_required = array_intersect_key($this->states, $states);
}
// Drupal already adds the required attribute, so we don't need to set the
// required rule.
if ($is_required || $is_conditionally_required) {
$message = $element['#required_error'] ?? $this
->t('@title field is required.', [
'@title' => $this
->getElementTitle($element),
]);
return [
'messages' => [
'required' => $message,
],
];
}
}