-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
ngf-model-invalid not compatible with ng-messages #1335
Comments
For now you can just have a pre processor to convert the $error to that format when the invalid model is changed. |
Yep, that'll have to do for now :) |
Also, for simplicity's sake, perhaps if multiple files is not enabled, |
As for watching the error object, I'm having trouble getting Angular to watch the object for changes. |
The files will not be returned until the errors are set so if you just watch the model object of file array they should already have the errors. |
You can also have |
I've tried to check the error object in the I've given up on this for now, so just resort to checking if the file passed in on select is |
Fixed at 12.0.1 use |
Thanks 👍 |
In case anyone else is trying to figure out how to make this work, I got it to work like this:
|
Well, the whole point was that it can be used when you are not using a file input element, but instead a drop area, or a button. In that case you have no @danialfarid could you clarify how we access the new |
Yes but since version 12.0.x |
How to use |
@michelem09 should be able to do the following now, please correct me if I'm wrong @danialfarid : $scope.invalidFile = {}; <div ngf-drop="selectedFile($file)" ngf-model-invalid="invalidFile"></div>
<div ng-messages="invalidFile.$errorMessages">
<div ng-message="maxSize">The file must be less than 2MB</div>
<div ng-message="minHeight">The file must be at least 100px high</div>
</div> This is when multiple files is not enabled. Otherwise, the $scope.invalidFiles = []; <div ngf-drop="selectedFile($file)" ngf-model-invalid="invalidFiles"></div>
<div ng-repeat="invalidFile in invalidFiles" ng-messages="invalidFile.$errorMessages">
<div ng-message="maxSize">The file must be less than 2MB</div>
<div ng-message="minHeight">The file must be at least 100px high</div>
</div> |
@adambuczynski Thank you for that answer! I have been attempting to make the error message go away if the user uses the correct files afterwards. Is there a way to add |
@austin4perezident I believe I just manually cleared/reset the error object when a new file was selected. It was the easiest way to do it from what I remember. |
This may be a bit over-kill for your project, but I found that you can create a custom validation directive to handle messaging outside ng-file-upload internal system that should work with ngMessages. It does require you to be using your own view-model services though for tracking file selection changes and applying those selected and adding them to your model. This takes a bit of work to setup right, but can be implemented in a view-model service by adding functions to the model, and calling those functions via ngf-select and/or ngf-before-model-change. When you specify the ng-model for ng-file-upload, that model basically becomes a temporary container for actions in your view-model service. Passing $file or $newFiles directly to your model function, applying those to your own file lists and then clearing the ng-model. (this does not work with ngf-keep; however, if you're using your own view-model service you don't want it to keep them anywayd ). You can also then use some of ng-file-upload validation to prevent undesired files from being selected in the first place. (ie: ngf-pattern: "'.jpg'" or ngf-min-height="1080") Creating a custom validation will also allow you to take advantage of MutationObservers to automatically detect an attribute change you want to be looking for on the element containing say, a thumbnail image. an example of a custom validation that gets attached to an img tag:
In HTML side for viewing the preview or thumbnail:
In HTML side for setting an 'ok' button that this form is valid:
Notice these:
You should now be able to use myimageValid.$error with ngMessages too... I haven't tested that, but it should work and is not longer dependent upon any standard 'form' validations (such as: text, number, url, email, date, radio, checkbox ) https://docs.angularjs.org/guide/forms (scroll down to "custom validation" ) "If" you needed to check the value or file against other parts of your view-model, it would be easy by injecting your view-model service into the directive... then you can call your service, get a value from some other view-model part.. and compare it to the changed element and provide your result. |
Hi, first of all, I'm very impressed with how far this plugin has come and how much is possible to do with it out of the box. It's matured a lot over the past year.
I have an issue I hope you can help with. I would like to use
ng-messages
to show validation errors coming out ofng-file-upload
. However, the only way to do that currently seems to be by using a form and file input field.My UI doesn't have that, instead I am using the file drop functionality in combination with a button to select files. So when browsing the documentation, I found the directive
ngf-model-invalid
, thinking it could be used for displaying errors withng-messages
.The format of the error model however is incompatible with
ng-messages
or with theform.field.$error
output; it expects something along the lines of:but instead it gets:
Could you make the latter compatible by setting the
$error
field to be an object hash with the relevant errors in there?Thanks.
The text was updated successfully, but these errors were encountered: