Dec 16, 2020 in Drupal 8

The form API in core is great, but without contributed modules it lacks a bit of sweetness for html 5. This snippet shows you how:

$form['price'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
        ' type' => 'number', // Note the space!
    ),
    '#title' => 'Price',
    '#required' => true,
    '#maxlength' => 3
);

 

Comments