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

Could you tell me the way to validate form when the input is clean. #10

Closed
euclid1990 opened this issue Apr 11, 2014 · 8 comments
Closed

Comments

@euclid1990
Copy link

Could you tell me the way to validate form when the input is clean.
I have a model $scope.form and it is empty object.
Now, I want to auto show validate message at initial screen.

Example: Althought user has not interacted with the form, but it show the message error
selection_007

@hueitan
Copy link
Owner

hueitan commented Apr 11, 2014

Hello @NguyenVanVuong

You can use submit $validationProvider.validate(form) to trigger the error message

Here's the example

<form name="Form">
    <input type="text" name="number" ng-model="form.number" validator="number"/>
    <button ng-click="form.submit(Form)"></button>
</form>
<script>
    $scope.form = {
        submit: function (form) {
            $validationProvider.validate(form)
                .success(successCallback)
                .error(errorCallback);
        }
    };
    // ...
</script>

@euclid1990
Copy link
Author

@huei90
I have found some mistake here:
I read file angular-validation.js.

https://github.com/huei90/angular-validation/blob/master/dist/angular-validation.js#L320

$validationProvider.getExpression(validation).test(value) ? valid.success() : valid.error();

But when initial with [required] validator, althought the field is empty but $validationProvider.getExpression(validation).test(value) still return true;

So i have to edit it be to:

if (typeof value == 'undefined' && validation == 'required') return valid.error();
return $validationProvider.getExpression(validation).test(value) ? valid.success() : valid.error();

Plz check regex of [required] validator

@hueitan
Copy link
Owner

hueitan commented Apr 11, 2014

@NguyenVanVuong

You are right , but angular-validation will check only iff $dirty status is true

That's means $pristine status doesn't do the validation.

Here's the reference

And I suggested you use submit to trigger validation to show error message.

@euclid1990
Copy link
Author

@huei90
I have try your code here but it not validate when init screen
http://plnkr.co/edit/MXf5eR2Mnclm46nMj6u2?p=preview

@hueitan
Copy link
Owner

hueitan commented Apr 11, 2014

Hi @NguyenVanVuong

Thanks for pointing out this problem.

Here's my solution

http://plnkr.co/edit/LMQXsQ?p=preview

You have to get the Form Constructor first, which is built using html form name

$timeout(function () {
    $validationProvider.validate($scope.Form);
});

And we should use $timeout to make sure Form Constructor is generated.

hueitan pushed a commit that referenced this issue Apr 11, 2014
@hueitan
Copy link
Owner

hueitan commented Apr 11, 2014

Thanks for your issue and I have added Q & A section in here

Thanks again!

_Perhaps I have to add a provider to easy this_

@hueitan hueitan added this to the 1.1.0 milestone Apr 12, 2014
@euclid1990
Copy link
Author

@huei90 Thank for support 👯

@hueitan hueitan closed this as completed Apr 12, 2014
@hueitan
Copy link
Owner

hueitan commented Apr 12, 2014

@NguyenVanVuong Issue me if you have other suggestion 😄
Thanks

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

No branches or pull requests

2 participants