Drupal is a registered trademark of Dries Buytaert
Release: Drupal 10.3.6 Update released for Drupal core (10.3.6)! Release: Drupal 11.0.5 Update released for Drupal core (11.0.5)! Release: Drupal 10.2.9 Update released for Drupal core (10.2.9)! Release: Drupal 10.2.10 Update released for Drupal core (10.2.10)! Release: Drupal 10.3.7 Update released for Drupal core (10.3.7)! Release: Drupal 11.0.6 Update released for Drupal core (11.0.6)! Release: Drupal 10.3.8 Update released for Drupal core (10.3.8)! Release: Drupal 11.0.7 Update released for Drupal core (11.0.7)! Module Revived: Translation Management Tool 8.x-1.19 Module tmgmt updated after 7 months of inactivity (8.x-1.19). Usage Milestone: Account field split Module account_field_split crossed 10,000 active installs.

Bring Laravel helpers function to Drupal. This module has many useful function for developer when working with Array and String. More detail about helpers read here:

Version 10.0.x with Laravel 10.x

https://laravel.com/docs/10.x/helpers

https://laravel.com/docs/10.x/strings

https://laravel.com/docs/10.x/collections

Version 11.0.x with Laravel 11.x

https://laravel.com/docs/11.x/helpers

https://laravel.com/docs/11.x/strings

https://laravel.com/docs/11.x/collections

Laravel Validation with Drupal form

Validate per field

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['name'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Name'),
      '#description' => $this->t('Input your name, ascii with number. Exclude aaa'),
      '#laravel_validators' => [
        'required',
        'alpha_num:ascii',
        function (string $attribute, mixed $value, \Closure $fail) use ($form_state) {
          if ($value === 'aaa') {
            $fail("The :attribute is invalid.");
          }
        },
      ],
    ];

    // Use custom validation class.
    $form['nick_name'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Nick Name'),
      '#description' => $this->t('Input your nick name, it required uppercase'),
      '#laravel_validators' => [
        'required',
        'string',
        new Uppercase(),
      ],
    ];

    $form['email'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Email'),
      '#description' => $this->t('Input your email.'),
      '#laravel_validators' => [
        'email' => $this->t(':attribute require valid email address'),
        'max:50',
      ],
    ];

    $form['age'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Age'),
      '#description' => $this->t('Input your age. Min 18, Max 50'),
      '#laravel_validators' => 'required|numeric|min:18|max:50',
    ];

    $form['favorite'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Favorite'),
      '#description' => $this->t('Input your favorite. It required when age = 18 or age > 30'),
      '#laravel_validators' => [
        new RequiredIf(function () use ($form, $form_state) {
          $age = $form_state->getUserInput()['age'];
          return $age && (intval($age) > 30 || intval($age) == 18);
        }),
        '#messages' => [
          'required' => ':attribute is required when age = 18 or age > 30',
        ],
      ],
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
    ];

    return $form;
  }

Validate with form

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['name'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Name'),
      '#description' => $this->t('Input your name, ascii with number. Exclude aaa'),
    ];

    // Use custom validation class.
    $form['nick_name'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Nick Name'),
      '#description' => $this->t('Input your nick name, it required uppercase'),
    ];

    $form['email'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Email'),
      '#description' => $this->t('Input your email.'),
    ];

    $form['age'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Age'),
      '#description' => $this->t('Input your age. Min 18, Max 50'),
    ];

    $form['favorite'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this->t('Favorite'),
      '#description' => $this->t('Input your favorite. It required when age = 18 or age > 30'),
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
    ];

    $form['#laravel_form_validators'] = [
      'name' => 'required|alpha_num:ascii',
      'nick_name' => 'required|string|uppercase',
      'email' => 'email|max:50',
      'age' => 'required|numeric|min:18|max:50',
      'favorite' => 'required_if:age,18',
      '#messages' => [
        'email.email' => ':attribute require valid email address',
        'favorite.required_if' => ':attribute need input when age = 18',
      ],
      '#attributes' => [
        'favorite' => 'Your Favorite',
      ],
    ];

    return $form;
  }

Validate custom data with ECA action

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
7
Tracked since
Nov 2024
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 7
Maintenance
Dormant

Release Timeline

Releases

Version Type Core Release date
10.0.2 Stable Nov 13, 2024
11.0.2 Stable Nov 13, 2024
11.0.1 Stable Nov 5, 2024
10.0.1 Stable Nov 5, 2024
2.0.1 Stable Nov 5, 2024
2.0.1-beta1 Pre-release Nov 5, 2024
2.0.x-dev Dev Nov 4, 2024