johannesjo/ng-fab-form

Validation messages displayed only for requried fields

Closed this issue · 7 comments

This code does not let validation messages to be displayed if field is not required. We can use
the custom template to change this, but I think this fix should in main template.

Could you elaborate? Do you want to display validation messages, if there is no required-attr. set?

e.g. For phone field I have added validation max-length to be 15, but it is not required field.
If someone adds data to this phone field it must not exceed the 15 chars.
So I will not add "required" attribute to the input.

In this scenario validation message does not get displayed for invalid inputs.

Thanks for the explanation. I think the default behaviour is still preferable, as usually a field is required, when it should be validated and vice versa and I could imagine it would be very annoying to have validation messages shown, where you don't want them and it is easier to conditionally add ng-required than to conditionally disable validations for a field.

You could use ng-required in the case you described above, e.g.:

<input ng-required="ngModel.length > 0">

I'm not 100% sure though. Maybe I'm wrong. If other people share your irritation I'm open to changing that.

Thanks for your reply., My opinion is that, validations are methods to allow "only valid" data to form.
I think that this case holds for both required and non-required fields.

I have already implemented workaround in my validation template to show messages even for non-required && invalid fields.

Lets see if anybody other than me has same problem which I think is the likely case.

:)

I'm closing this for now, but feel free to reopen. Thanks for your input again!

Well, I also disagree that only required fields should show validation messages. In my specific case, I have a validation that checks if the user's birthdate is either empty or a valid date. It isn't required but if the user fills it, it should be validated and I don't want to rely on browser's specifics.

Let me ask a question tho, what problems would arise if I removed the ng-show="attrs.required===''|| attrs.required" attribute? I've tested and everything seems right. Is there any scenario that this could cause a problem?

@lhmiranda thanks for your input! There is always the option to just change the validations template to one of your liking. It's very simple and explained in the Readme. I'm not very keen about changing the default behavior to be honest, as it might break the existing functionality for some people.