Skip to content
Snippets Groups Projects

Issue #3471145 - Fix all callbacks

3 files
+ 10
59
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -4,7 +4,6 @@ namespace Drupal\fapiv_example\Form;
@@ -4,7 +4,6 @@ namespace Drupal\fapiv_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
/**
/**
* Implements the SimpleForm form controller.
* Implements the SimpleForm form controller.
@@ -17,43 +16,21 @@ use Drupal\Core\Messenger\MessengerInterface;
@@ -17,43 +16,21 @@ use Drupal\Core\Messenger\MessengerInterface;
class SimpleForm extends FormBase {
class SimpleForm extends FormBase {
/**
/**
* Protected member variable.
* {@inheritdoc}
*
* @var \Drupal\Component\Utility\MessengerInterface
*/
*/
protected $messenger;
public function getFormId() {
return 'fapiv_example_simple_form';
/**
* Constructs a new SimpleForm.
*
* @param \Drupal\Component\Utility\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
}
/**
/**
* Build the simple form.
* {@inheritdoc}
*
* 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.
*/
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state) {
$form['title'] = [
$form['title'] = [
'#type' => 'textfield',
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#title' => $this->t('Title'),
'#description' => $this->t('Title must be at least 5 characters in length.'),
'#description' => $this->t('Title must be at least 5 characters in length.'),
'#validators' => ['length[5, *]'],
'#validators' => ['rule' => 'length[5, *]'],
'#filters' => ['uppercase', 'trim'],
'#filters' => ['uppercase', 'trim'],
'#required' => TRUE,
'#required' => TRUE,
];
];
@@ -64,7 +41,6 @@ class SimpleForm extends FormBase {
@@ -64,7 +41,6 @@ class SimpleForm extends FormBase {
'#description' => $this->t('The Value should be JonhDoe.'),
'#description' => $this->t('The Value should be JonhDoe.'),
'#validators' => [
'#validators' => [
['rule' => 'length[7]', 'error' => 'Wrong name size of field %field.'],
['rule' => 'length[7]', 'error' => 'Wrong name size of field %field.'],
'custom_validator',
],
],
'#required' => TRUE,
'#required' => TRUE,
];
];
@@ -86,34 +62,9 @@ class SimpleForm extends FormBase {
@@ -86,34 +62,9 @@ class SimpleForm extends FormBase {
}
}
/**
/**
* Getter method for Form ID.
* {@inheritdoc}
*
* 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.
*/
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
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');
$title = $form_state->getValue('title');
$this->messenger()->addMessage($this->t('You specified a title of %title.', ['%title' => $title]));
$this->messenger()->addMessage($this->t('You specified a title of %title.', ['%title' => $title]));
}
}
Loading