Angular 5 manually set Error in reactive form is not reflecting on dom (IE/IOS)












4















I'm working with angular 5 reactive form.
In that I have one browse field, On change event of that browse button I'm calling a function, in that file validation is checked.
If file is invalid, I'm setting an error manually to that field.



Now what I'm expecting is when file input error is set. Error message should display in DOM. It's working fine with Chrome, Firefox and Android devices. But It is not working in IE and IOS.



HTML CODE



<div class="form-group" [ngClass]="{'has-error': hasError('profileimage')}">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage"
(change)="fileUpload($event.target.files,'profileimage')"
type="file"
formControlName="profileimage">
<span class="is-error" *ngIf="checkValidImageFormat('profileimage')">
Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
<span class="is-error" *ngIf="checkValidImageSize('profileimage')">File must be less than 1 MB .
</span>
</div>


Component.ts



fileUpload(files: FileList, field) {
this.profileimage = files[0];
let extension = this.profileimage.name.match(/(?:.([^.]+))?$/)[1];
//checking the extension of the file
if (extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png') {

var sizeInMB = (this.profileimage.size / (1024 * 1024)).toFixed(2);
//checking the file size should be less than 1 mb
if (!parseFloat(sizeInMB) < 1) {
this.contactForm.controls[field].setErrors({ 'invalid_size': true });
}
}

checkValidImageSize(fieldName) {
return this.contactForm.controls[fieldName].errors.invalid_size;
}


I have tried all change detection strategies (NgZone, ChangeDetectorRef etc.) for angular but none of them worked.
Any help would be appreciated.










share|improve this question























  • have a look at here

    – Prashant Pimpale
    Nov 26 '18 at 14:15











  • Are there any console errors in IE?

    – noamyg
    Nov 26 '18 at 14:20
















4















I'm working with angular 5 reactive form.
In that I have one browse field, On change event of that browse button I'm calling a function, in that file validation is checked.
If file is invalid, I'm setting an error manually to that field.



Now what I'm expecting is when file input error is set. Error message should display in DOM. It's working fine with Chrome, Firefox and Android devices. But It is not working in IE and IOS.



HTML CODE



<div class="form-group" [ngClass]="{'has-error': hasError('profileimage')}">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage"
(change)="fileUpload($event.target.files,'profileimage')"
type="file"
formControlName="profileimage">
<span class="is-error" *ngIf="checkValidImageFormat('profileimage')">
Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
<span class="is-error" *ngIf="checkValidImageSize('profileimage')">File must be less than 1 MB .
</span>
</div>


Component.ts



fileUpload(files: FileList, field) {
this.profileimage = files[0];
let extension = this.profileimage.name.match(/(?:.([^.]+))?$/)[1];
//checking the extension of the file
if (extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png') {

var sizeInMB = (this.profileimage.size / (1024 * 1024)).toFixed(2);
//checking the file size should be less than 1 mb
if (!parseFloat(sizeInMB) < 1) {
this.contactForm.controls[field].setErrors({ 'invalid_size': true });
}
}

checkValidImageSize(fieldName) {
return this.contactForm.controls[fieldName].errors.invalid_size;
}


I have tried all change detection strategies (NgZone, ChangeDetectorRef etc.) for angular but none of them worked.
Any help would be appreciated.










share|improve this question























  • have a look at here

    – Prashant Pimpale
    Nov 26 '18 at 14:15











  • Are there any console errors in IE?

    – noamyg
    Nov 26 '18 at 14:20














4












4








4








I'm working with angular 5 reactive form.
In that I have one browse field, On change event of that browse button I'm calling a function, in that file validation is checked.
If file is invalid, I'm setting an error manually to that field.



Now what I'm expecting is when file input error is set. Error message should display in DOM. It's working fine with Chrome, Firefox and Android devices. But It is not working in IE and IOS.



HTML CODE



<div class="form-group" [ngClass]="{'has-error': hasError('profileimage')}">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage"
(change)="fileUpload($event.target.files,'profileimage')"
type="file"
formControlName="profileimage">
<span class="is-error" *ngIf="checkValidImageFormat('profileimage')">
Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
<span class="is-error" *ngIf="checkValidImageSize('profileimage')">File must be less than 1 MB .
</span>
</div>


Component.ts



fileUpload(files: FileList, field) {
this.profileimage = files[0];
let extension = this.profileimage.name.match(/(?:.([^.]+))?$/)[1];
//checking the extension of the file
if (extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png') {

var sizeInMB = (this.profileimage.size / (1024 * 1024)).toFixed(2);
//checking the file size should be less than 1 mb
if (!parseFloat(sizeInMB) < 1) {
this.contactForm.controls[field].setErrors({ 'invalid_size': true });
}
}

checkValidImageSize(fieldName) {
return this.contactForm.controls[fieldName].errors.invalid_size;
}


I have tried all change detection strategies (NgZone, ChangeDetectorRef etc.) for angular but none of them worked.
Any help would be appreciated.










share|improve this question














I'm working with angular 5 reactive form.
In that I have one browse field, On change event of that browse button I'm calling a function, in that file validation is checked.
If file is invalid, I'm setting an error manually to that field.



Now what I'm expecting is when file input error is set. Error message should display in DOM. It's working fine with Chrome, Firefox and Android devices. But It is not working in IE and IOS.



HTML CODE



<div class="form-group" [ngClass]="{'has-error': hasError('profileimage')}">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage"
(change)="fileUpload($event.target.files,'profileimage')"
type="file"
formControlName="profileimage">
<span class="is-error" *ngIf="checkValidImageFormat('profileimage')">
Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
<span class="is-error" *ngIf="checkValidImageSize('profileimage')">File must be less than 1 MB .
</span>
</div>


Component.ts



fileUpload(files: FileList, field) {
this.profileimage = files[0];
let extension = this.profileimage.name.match(/(?:.([^.]+))?$/)[1];
//checking the extension of the file
if (extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png') {

var sizeInMB = (this.profileimage.size / (1024 * 1024)).toFixed(2);
//checking the file size should be less than 1 mb
if (!parseFloat(sizeInMB) < 1) {
this.contactForm.controls[field].setErrors({ 'invalid_size': true });
}
}

checkValidImageSize(fieldName) {
return this.contactForm.controls[fieldName].errors.invalid_size;
}


I have tried all change detection strategies (NgZone, ChangeDetectorRef etc.) for angular but none of them worked.
Any help would be appreciated.







javascript angular angular-reactive-forms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 14:05









Ushma JoshiUshma Joshi

314210




314210













  • have a look at here

    – Prashant Pimpale
    Nov 26 '18 at 14:15











  • Are there any console errors in IE?

    – noamyg
    Nov 26 '18 at 14:20



















  • have a look at here

    – Prashant Pimpale
    Nov 26 '18 at 14:15











  • Are there any console errors in IE?

    – noamyg
    Nov 26 '18 at 14:20

















have a look at here

– Prashant Pimpale
Nov 26 '18 at 14:15





have a look at here

– Prashant Pimpale
Nov 26 '18 at 14:15













Are there any console errors in IE?

– noamyg
Nov 26 '18 at 14:20





Are there any console errors in IE?

– noamyg
Nov 26 '18 at 14:20












1 Answer
1






active

oldest

votes


















3














You can create attribute directive some thing like...



html (you must follow this order of html tags)



 <div class="form-group">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage" checkinvalid (change)="fileUpload($event.target.files,'profileimage')" type="file"
formControlName="profileimage">
<span class="is-error">File must be less than 1 MB .</span>
<span class="is-error">Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
</div>


component.ts (not necessary because you already get value in form control)



fileUpload(files: FileList, field) {
this.profileimage = files[0];
}


directive.ts



parent: any;
spanForFormat: any;
spanForSize: any;

@HostListener('change', ['$event'])
handleChangeEvent(event) {
const file = event.target.files[0];
this.checkInvalid(file);
}

constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

ngOnInit() {
this.parent = this.elementRef.nativeElement['parentElement'];
this.spanForSize = this.elementRef.nativeElement['parentElement'].children[2];
this.spanForFormat = this.elementRef.nativeElement['parentElement'].children[3];
this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
}

private checkInvalid(file) {

this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
this.renderer.removeClass(this.parent, 'has-error');

let extension = file.name.match(/(?:.([^.]+))?$/)[1];
let sizeInMB = (file.size / (1024 * 1024)).toFixed(2);

if (!(extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png')) {
this.renderer.removeClass(this.spanForFormat, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}

if (!(parseFloat(sizeInMB) < 1)) {
this.renderer.removeClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}
}





share|improve this answer
























  • This is not working.

    – Ushma Joshi
    Nov 28 '18 at 10:17











  • Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

    – R. Viral
    Nov 28 '18 at 12:11













  • Oh, thanks, I have changed some code and It's working.

    – Ushma Joshi
    Nov 28 '18 at 13:34











  • you'r welcome..

    – R. Viral
    Nov 29 '18 at 5:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482844%2fangular-5-manually-set-error-in-reactive-form-is-not-reflecting-on-dom-ie-ios%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can create attribute directive some thing like...



html (you must follow this order of html tags)



 <div class="form-group">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage" checkinvalid (change)="fileUpload($event.target.files,'profileimage')" type="file"
formControlName="profileimage">
<span class="is-error">File must be less than 1 MB .</span>
<span class="is-error">Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
</div>


component.ts (not necessary because you already get value in form control)



fileUpload(files: FileList, field) {
this.profileimage = files[0];
}


directive.ts



parent: any;
spanForFormat: any;
spanForSize: any;

@HostListener('change', ['$event'])
handleChangeEvent(event) {
const file = event.target.files[0];
this.checkInvalid(file);
}

constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

ngOnInit() {
this.parent = this.elementRef.nativeElement['parentElement'];
this.spanForSize = this.elementRef.nativeElement['parentElement'].children[2];
this.spanForFormat = this.elementRef.nativeElement['parentElement'].children[3];
this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
}

private checkInvalid(file) {

this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
this.renderer.removeClass(this.parent, 'has-error');

let extension = file.name.match(/(?:.([^.]+))?$/)[1];
let sizeInMB = (file.size / (1024 * 1024)).toFixed(2);

if (!(extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png')) {
this.renderer.removeClass(this.spanForFormat, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}

if (!(parseFloat(sizeInMB) < 1)) {
this.renderer.removeClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}
}





share|improve this answer
























  • This is not working.

    – Ushma Joshi
    Nov 28 '18 at 10:17











  • Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

    – R. Viral
    Nov 28 '18 at 12:11













  • Oh, thanks, I have changed some code and It's working.

    – Ushma Joshi
    Nov 28 '18 at 13:34











  • you'r welcome..

    – R. Viral
    Nov 29 '18 at 5:43
















3














You can create attribute directive some thing like...



html (you must follow this order of html tags)



 <div class="form-group">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage" checkinvalid (change)="fileUpload($event.target.files,'profileimage')" type="file"
formControlName="profileimage">
<span class="is-error">File must be less than 1 MB .</span>
<span class="is-error">Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
</div>


component.ts (not necessary because you already get value in form control)



fileUpload(files: FileList, field) {
this.profileimage = files[0];
}


directive.ts



parent: any;
spanForFormat: any;
spanForSize: any;

@HostListener('change', ['$event'])
handleChangeEvent(event) {
const file = event.target.files[0];
this.checkInvalid(file);
}

constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

ngOnInit() {
this.parent = this.elementRef.nativeElement['parentElement'];
this.spanForSize = this.elementRef.nativeElement['parentElement'].children[2];
this.spanForFormat = this.elementRef.nativeElement['parentElement'].children[3];
this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
}

private checkInvalid(file) {

this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
this.renderer.removeClass(this.parent, 'has-error');

let extension = file.name.match(/(?:.([^.]+))?$/)[1];
let sizeInMB = (file.size / (1024 * 1024)).toFixed(2);

if (!(extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png')) {
this.renderer.removeClass(this.spanForFormat, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}

if (!(parseFloat(sizeInMB) < 1)) {
this.renderer.removeClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}
}





share|improve this answer
























  • This is not working.

    – Ushma Joshi
    Nov 28 '18 at 10:17











  • Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

    – R. Viral
    Nov 28 '18 at 12:11













  • Oh, thanks, I have changed some code and It's working.

    – Ushma Joshi
    Nov 28 '18 at 13:34











  • you'r welcome..

    – R. Viral
    Nov 29 '18 at 5:43














3












3








3







You can create attribute directive some thing like...



html (you must follow this order of html tags)



 <div class="form-group">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage" checkinvalid (change)="fileUpload($event.target.files,'profileimage')" type="file"
formControlName="profileimage">
<span class="is-error">File must be less than 1 MB .</span>
<span class="is-error">Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
</div>


component.ts (not necessary because you already get value in form control)



fileUpload(files: FileList, field) {
this.profileimage = files[0];
}


directive.ts



parent: any;
spanForFormat: any;
spanForSize: any;

@HostListener('change', ['$event'])
handleChangeEvent(event) {
const file = event.target.files[0];
this.checkInvalid(file);
}

constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

ngOnInit() {
this.parent = this.elementRef.nativeElement['parentElement'];
this.spanForSize = this.elementRef.nativeElement['parentElement'].children[2];
this.spanForFormat = this.elementRef.nativeElement['parentElement'].children[3];
this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
}

private checkInvalid(file) {

this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
this.renderer.removeClass(this.parent, 'has-error');

let extension = file.name.match(/(?:.([^.]+))?$/)[1];
let sizeInMB = (file.size / (1024 * 1024)).toFixed(2);

if (!(extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png')) {
this.renderer.removeClass(this.spanForFormat, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}

if (!(parseFloat(sizeInMB) < 1)) {
this.renderer.removeClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}
}





share|improve this answer













You can create attribute directive some thing like...



html (you must follow this order of html tags)



 <div class="form-group">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage" checkinvalid (change)="fileUpload($event.target.files,'profileimage')" type="file"
formControlName="profileimage">
<span class="is-error">File must be less than 1 MB .</span>
<span class="is-error">Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
</div>


component.ts (not necessary because you already get value in form control)



fileUpload(files: FileList, field) {
this.profileimage = files[0];
}


directive.ts



parent: any;
spanForFormat: any;
spanForSize: any;

@HostListener('change', ['$event'])
handleChangeEvent(event) {
const file = event.target.files[0];
this.checkInvalid(file);
}

constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

ngOnInit() {
this.parent = this.elementRef.nativeElement['parentElement'];
this.spanForSize = this.elementRef.nativeElement['parentElement'].children[2];
this.spanForFormat = this.elementRef.nativeElement['parentElement'].children[3];
this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
}

private checkInvalid(file) {

this.renderer.addClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.spanForFormat, 'hidden');
this.renderer.removeClass(this.parent, 'has-error');

let extension = file.name.match(/(?:.([^.]+))?$/)[1];
let sizeInMB = (file.size / (1024 * 1024)).toFixed(2);

if (!(extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png')) {
this.renderer.removeClass(this.spanForFormat, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}

if (!(parseFloat(sizeInMB) < 1)) {
this.renderer.removeClass(this.spanForSize, 'hidden');
this.renderer.addClass(this.parent, 'has-error');
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 13:37









R. ViralR. Viral

2175




2175













  • This is not working.

    – Ushma Joshi
    Nov 28 '18 at 10:17











  • Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

    – R. Viral
    Nov 28 '18 at 12:11













  • Oh, thanks, I have changed some code and It's working.

    – Ushma Joshi
    Nov 28 '18 at 13:34











  • you'r welcome..

    – R. Viral
    Nov 29 '18 at 5:43



















  • This is not working.

    – Ushma Joshi
    Nov 28 '18 at 10:17











  • Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

    – R. Viral
    Nov 28 '18 at 12:11













  • Oh, thanks, I have changed some code and It's working.

    – Ushma Joshi
    Nov 28 '18 at 13:34











  • you'r welcome..

    – R. Viral
    Nov 29 '18 at 5:43

















This is not working.

– Ushma Joshi
Nov 28 '18 at 10:17





This is not working.

– Ushma Joshi
Nov 28 '18 at 10:17













Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

– R. Viral
Nov 28 '18 at 12:11







Above code not working on IE or not working on any browser. Let me share running example. tested on IE11 check running example

– R. Viral
Nov 28 '18 at 12:11















Oh, thanks, I have changed some code and It's working.

– Ushma Joshi
Nov 28 '18 at 13:34





Oh, thanks, I have changed some code and It's working.

– Ushma Joshi
Nov 28 '18 at 13:34













you'r welcome..

– R. Viral
Nov 29 '18 at 5:43





you'r welcome..

– R. Viral
Nov 29 '18 at 5:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482844%2fangular-5-manually-set-error-in-reactive-form-is-not-reflecting-on-dom-ie-ios%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista