Form fields attributes

Hi. Is there a way to quickly add attribute depending on Validators we set on a field? Like there is a PresenceOf validators so the field will render with required attributes. Or we set a regex, the field render with a pattern attributes.

This could help me doing some first validation" in js.


    new \Phalcon\Validation\Validator\PresenceOf(['message' => $translations->required])
$field->setAttribute('required', 'required');
$field->setAttribute('aria-required', 'true');

edited 11d ago

Yeah I know you can do that, but the PresenceOf Validator implies the attribut required, no ? Thanks for the answer anyways.

I made it quiclkly into a function on my baseForm, maybe it's not optimized for now.

    public function isFieldRequired($field)
        if ($this->has($field)) {
            $element = $this->get($field);
            if ($element->getValidators()){
                foreach ($element->getValidators() as $validator){
                    if ($validator instanceof PresenceOf){
                        //We have a PresenceOf validator, so the input is mandatory
                        return true;
        return false;

Validator class has nothing to do with inputs, of course you can use it to validate form fields, but you can simple pass an array from CSV file for example and validate it.