-
Notifications
You must be signed in to change notification settings - Fork 650
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
Validation on submit #27
Comments
Hi! There is no full validation before submit, per design I didn't want schema form to mess around with the submit part, since there might not be one. For instance we're using it and saving any value on 'blur' of the field. The idea was to keep it "angularish" and that the user could use ng-submit if they wanted and rely on using the form controller (i.e. name on a form tag, https://docs.angularjs.org/api/ng/directive/form) to check if the entire form is valid or not, as you would with any kind of validation in angular. So basically thats the way to check if the form is valid or not. Does that make sense? Should probably be documented better :-) There is a fine point here though, if you omit fields in your form definition, the form might be valid, but if you validate the model object against the schema it is not valid, which can be a problem if you validate on the backend. |
Thx for this response David! I think you are right, maybe I wanted to over engineer the whole form part and forgot that its anyway only client side validation. Thx for the tip how to use form validation in angular. |
I'm just wondering whats the angular way to display the errors on submit. The problem are the fields that are unchanged but invalid. In my submit function i now set the form-controller $dirty and loop over the form-controllers error-fields and set them to $dirty (since the $setDirty() doesn't propagate to child fields).
Probably this should be done with a directive that intercepts the submit event and set a $submitted flag or somethin? But then I would have to change the hasError() code in the schema-validate directive. |
Good question! I don't have a good answer though :( Currently we hook up a "disabled" attribute on the submit button to formCtrl.$invalid so we just don't run into that problem. |
So there is no way to know the validity of the whole form ? |
@chachou29 there is! Just check what the form controller says, as you would do with any form in angular. See my comment above. |
@davidlgj Yes, with the required option it should work on most browsers. I just like it better to disable the browser validation and have full control. Also I think it would not be nice to disable the submit button, since the user doesn't know then what is wrong. I like this approach here (from section "Manually Checking for Errors" downwards): http://blog.yodersolutions.com/bootstrap-form-validation-done-right-in-angularjs/ Does this look reasonable to you? |
@torstenrudolf I think it looks very reasonable, although I disagree about disabling the submit button always is a bad thing. If the form has clearly marked "required" fields, then it is clear for the user, either they haven't filled in the entire form or they have a big validation error message to read. Also validating as you type can be both great and a bit of a bother, great when the first invalid character in a "username" directly prompts the user with the rules, but the email validation that just goes wrong is not that fun. That said schema form should obviously try to support doing "manual" validation on submit and validation on blur instead of on change. So IMHO a couple of things are needed
Do you see anything missing? |
Hello David, |
@chachou29 But "MyForm" is defined? Could you make a jsfiddle or send me an example? Mail me at david.lgj@gmail.com |
@chachou29 : I assume <form name="myForm" ...>
form is valid: {{ myForm.$valid }} // that should work
</form>
form is valid: {{ myForm.$valid }} // that should not work |
@davidlgj I would really like the option to validate on blur! Still I think clicking the "submit" button and then having the required fields highlighted, but probably everyone has it's own taste there ;) at 1: I think to manage the validation of the form by an event is the right way. <form ... sf-validation-on-blur="true"></form> |
Hello, @torstenrudolf |
@chachou29 maybe this helps: http://jsfiddle.net/JV7L4/29/ |
Hi @chachou29, (and thanks for helping out @torstenrudolf) sorry it took so long to answer. The problem is with scoping. I was a bit surprised that the first version works for you at all, I usually find that the form controller exposed by setting the name attribute on form only is available "inside" the form, i.e. on the scope the form directive introduces. But since you fiddle works I guess the form directive does not create a scope in its own after all. A solution to working with the forms controller is to not put the That said you actually found a bug :-) A bit embarrassing bug actually... Let me first explain what the The bug is that |
Hi @chachou29 and @torstenrudolf, there are some commit to development that fixes some of these issues. Basically you can issue a Also I have made sort of a solution to validating on blur instead of on change. Basically it boils down to this: Angular 1.3 has support for it with the directive ngModelOptions so I've added it and made it so you can set it via the form definition per field or via a global setting for the entire form. Hope that works for you. |
Ok, so it's out now. Check the docs https://github.com/Textalk/angular-schema-form/blob/master/docs/index.md#handling-submit |
Thanks @davidlgj !! |
It works like a charm! |
I have a question regarding the validation process.
I tried to dig through the code, but I'm not sure if I got it right. I'm also pretty new to angular.
In my examples it seems like, I can submit the form even that there are validation issues.
Is there a full validation before the form gets submitted?
I think that would be a good improvement if not.
Also, can I call a function, that goes through the schema and validates the fields and updates the valid state of the form fields?
The text was updated successfully, but these errors were encountered: