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
Laravel validate

Project information

Releases