Skip to content
Snippets Groups Projects
Commit c19247d4 authored by Viktor Holovachek's avatar Viktor Holovachek
Browse files

Issue #3471145 - Fix all callbacks

parent c78f5cf7
No related branches found
No related tags found
1 merge request!6Issue #3471145 - Fix all callbacks
......@@ -4,7 +4,6 @@ namespace Drupal\fapiv_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
/**
* Implements the SimpleForm form controller.
......@@ -17,43 +16,21 @@ use Drupal\Core\Messenger\MessengerInterface;
class SimpleForm extends FormBase {
/**
* Protected member variable.
*
* @var \Drupal\Component\Utility\MessengerInterface
* {@inheritdoc}
*/
protected $messenger;
/**
* Constructs a new SimpleForm.
*
* @param \Drupal\Component\Utility\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
public function getFormId() {
return 'fapiv_example_simple_form';
}
/**
* Build the simple form.
*
* A build form method constructs an array that defines how markup and
* other form elements are included in an HTML form.
*
* @param array $form
* Default form array structure.
* @param Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*
* @return array
* The render array defining the elements of the form.
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['title'] = [
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#description' => $this->t('Title must be at least 5 characters in length.'),
'#validators' => ['length[5, *]'],
'#validators' => ['rule' => 'length[5, *]'],
'#filters' => ['uppercase', 'trim'],
'#required' => TRUE,
];
......@@ -64,7 +41,6 @@ class SimpleForm extends FormBase {
'#description' => $this->t('The Value should be JonhDoe.'),
'#validators' => [
['rule' => 'length[7]', 'error' => 'Wrong name size of field %field.'],
'custom_validator',
],
'#required' => TRUE,
];
......@@ -86,34 +62,9 @@ class SimpleForm extends FormBase {
}
/**
* Getter method for Form ID.
*
* The form ID is used in implementations of hook_form_alter() to allow other
* modules to alter the render array built by this form controller. it must
* be unique site wide. It normally starts with the providing module's name.
*
* @return string
* The unique ID of the form defined by this class.
*/
public function getFormId() {
return 'fapiv_example_simple_form';
}
/**
* Implements a form submit handler.
*
* The submitForm method is the default method called for any submit elements.
*
* @param array $form
* The render array of the currently built form.
* @param Drupal\Core\Form\FormStateInterface $form_state
* Object describing the current state of the form.
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/*
* This would normally be replaced by code that actually does something
* with the title.
*/
$title = $form_state->getValue('title');
$this->messenger()->addMessage($this->t('You specified a title of %title.', ['%title' => $title]));
}
......
......@@ -112,7 +112,7 @@ class FapiValidationValidatorsManager extends DefaultPluginManager {
return call_user_func_array([$plugin['class'], $plugin['error_callback']], [$validator, $element]);
}
// Plugin defined error message?
elseif ($plugin['error_message'] !== NULL) {
elseif (!empty($plugin['error_message'])) {
$message = $plugin['error_message'];
}
else {
......
......@@ -68,11 +68,11 @@ class Validator {
private function parse() {
if (is_array($this->rawValidator)) {
if (isset($this->rawValidator['error'])) {
$this->error_message = $this->rawValidator['error'];
$this->errorMessage = $this->rawValidator['error'];
}
if (isset($this->rawValidator['error callback'])) {
$this->error_callback = $this->rawValidator['error callback'];
$this->errorCallback = $this->rawValidator['error callback'];
}
if (!isset($this->rawValidator['rule'])) {
......@@ -153,7 +153,7 @@ class Validator {
* Check.
*/
public function hasErrorCallbackDefined() {
return $this->errorMessage !== NULL;
return $this->errorCallback !== NULL;
}
/**
......
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