Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many constraints validation per fields #24

Open
silasrm opened this issue Mar 13, 2017 · 9 comments
Open

Many constraints validation per fields #24

silasrm opened this issue Mar 13, 2017 · 9 comments

Comments

@silasrm
Copy link

silasrm commented Mar 13, 2017

Hi @Gregwar,

I've a case with 2 constraints but only 1 is returned in check:

$form2->addConstraint('quantidade_assentos', function($form) {
            if (empty($form->getValue('quantidade_assentos'))) {
                return 'Quantidade de assentos é obrigatória!';
            }

            return null;
        });

        $form2->addConstraint('quantidade_assentos', function($form) {
            if (!is_numeric($form->getValue('quantidade_assentos'))) {
                return 'Quantidade de assentos deve ser número inteiro!';
            }

            return null;
        });

Dump of getFields():

"quantidade_assentos" => NumberField {#613 ▼
    #type: "number"
    #min: "1"
    #max: null
    #step: "1"
    #name: "quantidade_assentos"
    #index: null
    #hook: null
    #attributes: array:3 [▶]
    #value: ""
    #required: true
    #regex: null
    #minlength: null
    #maxlength: null
    #prettyname: null
    #readonly: false
    #valueChanged: true
    #constraints: array:2 [▼
      0 => Closure {#606 ▶}
      1 => Closure {#605 ▶}
    ]
    #mapping: null
    #language: English {#625 ▶}
  }

Dump of errors:

array:1 [▼
  0 => Error {#604 ▼
    -field: NumberField {#613 ▶}
    -message: array:2 [▼
      0 => "value_required"
      1 => "quantidade_assentos"
    ]
    #language: English {#625 ▶}
  }
]

In my view, there should be 2 errors because a empty value "" is catched in both constraints.

Any error in my logic?

@Gregwar
Copy link
Owner

Gregwar commented Mar 13, 2017

Hello,

I belive you are correct, however the first error stops the check now as defined here
https://github.com/Gregwar/Formidable/blob/master/Fields/Field.php#L210

Also, what you want here seems to be the html5 number field

@Gregwar
Copy link
Owner

Gregwar commented Mar 13, 2017

(Which is handled in Formidable)

@silasrm
Copy link
Author

silasrm commented Mar 13, 2017

Hi @Gregwar,

I'm using html5 number field yes, but I desire to have many constraints and return many errors messages.

I'm using Zend_Form and desire and work in migration to Formidable. But this is a big problem for me because my forms has many validators(in Zend_Form)/constraints.

@Gregwar
Copy link
Owner

Gregwar commented Mar 13, 2017 via email

@silasrm
Copy link
Author

silasrm commented Mar 13, 2017

Many thanks!

@silasrm
Copy link
Author

silasrm commented Apr 30, 2017

Hi @Gregwar,

Any change?

Thk's

@Gregwar
Copy link
Owner

Gregwar commented May 1, 2017

Actually I think that this is harder than it looks
For instance, you don't want to have both messages "You should enter a value" and "The value should be an int", right?

@Gregwar
Copy link
Owner

Gregwar commented May 1, 2017

Maybe that should be an option, like retrieving all the errors or the first one of every field

@chippyash
Copy link
Collaborator

@silasrm Is this issue still relevant or can it be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants