In fact, form validation is based from a component, named "Validator Component".
You can often use the dedicated service if you did't have to show a form in a template. Like APIs. You may validate datas in the same way, like this :
For example, based on symfony doc :
$validator = $thi...