Form field validator Is there a way to validate fields individually like you would've done for the file upload (Eg: file_validate_extension etc)? I want to do something like this:

$form["field_name"] = array(
  "#type" =&gt; "textfield",
  ...
  "#field_validate" = array("field_validate_numeric") // or "field_validate_email" or "field_validate_url" etc
);


I know there are new html5 field types but I want to be able to validate textfields.

Also, I need this because I might not know the actual tree structure or field parents and I want to validate the field no matter how the form looks.
You can do this with a callback, so that it will work whereever you use it in the form tree:

Add this to your form element:

'#element_validate' =&gt; [[get_class($this), 'validateNumber']],


And put the callback in the form class:

public static function validateNumber(&amp;$element, FormStateInterface $form_state, &amp;$complete_form) {
    $value = $element['#value'];
    if ($value === '') {
      return;
    }

    $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];

    // Ensure the input is numeric.
    if (!is_numeric($value)) {
      $form_state-&gt;setError($element, t('%name must be a number.', array('%name' =&gt; $name)));
      return;
    }
}


(for the callback I've used some of the code from the html5 number element)