Angular: Check if a form is valid from outside the form (and show the error messages)












1















I have a form but i'm using a button outside the form to submit the data to an API.
The form has error messages for required fields, and hey are shown then a user selects a input but doesn't fill in any data (default HTML5 validation).



Since i'm not actually submitting the form, is there a way to show the same validation messages when the user clicks the button outside the form?



UPDATE: Added some code to further explain the isse. Also, i forgot to mention that i'm using Material Design for Bootstrap as my CSS framework.



Form HTML (component.html):



<form class="custom-form p-2">
<div class="row">
<div class="col-sm-6">
<div class="md-form form-sm">
<input mdbInputDirective type="text" id="title" [(ngModel)]="dataService.catalog.title" name="title"
value="{{ dataService.catalog.title }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="form-control">
<label for="title">Title</label>
</div>
</div>
<div class="col-sm-2">
<div class="md-form form-sm">
<input mdbInputDirective type="date" id="date" [ngModel]="dataService.catalog.date | date:'yyyy-MM-dd'"
name="date" (ngModelChange)="date = $event" required [validateSuccess]="false"
placeholder="" data-error="This field is mandatory" class="form-control">
<label for="date">Date</label>
</div>
</div>
<div class="col-sm-12">
<div class="md-form form-sm">
<textarea type="text" id="description" [(ngModel)]="dataService.catalog.description" name="description"
value="{{ dataService.catalog.description }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="md-textarea form-control" mdbInputDirective rows="3"></textarea>
<label for="description">Description</label>
</div>
</div>
</div>
</form>


Button HTML (component.html):



<button mdbBtn type="button" color="success" class="waves-light" size="sm" mdbWavesEffect (click)="create()">
<i class="fa fa-plus-square mr-sm-2 mr-1"></i> Create Catalog
</button>


Create Method (component.ts):



create() {
//TODO: call the form's own validation method to show the erros?!

if(formValid) {
this.dataService.create(this.dataService.catalog).subscribe(value => {
console.log(value);
}, error => {
console.log(error);
});
}
}









share|improve this question

























  • can you show us something you've tried so that we can help ?

    – CruelEngine
    Jan 19 at 4:15











  • Updated my question with some code samples. The idea is to call the forms own validation methods and not create a custom validation mechanism.

    – Ricky
    Jan 19 at 10:33
















1















I have a form but i'm using a button outside the form to submit the data to an API.
The form has error messages for required fields, and hey are shown then a user selects a input but doesn't fill in any data (default HTML5 validation).



Since i'm not actually submitting the form, is there a way to show the same validation messages when the user clicks the button outside the form?



UPDATE: Added some code to further explain the isse. Also, i forgot to mention that i'm using Material Design for Bootstrap as my CSS framework.



Form HTML (component.html):



<form class="custom-form p-2">
<div class="row">
<div class="col-sm-6">
<div class="md-form form-sm">
<input mdbInputDirective type="text" id="title" [(ngModel)]="dataService.catalog.title" name="title"
value="{{ dataService.catalog.title }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="form-control">
<label for="title">Title</label>
</div>
</div>
<div class="col-sm-2">
<div class="md-form form-sm">
<input mdbInputDirective type="date" id="date" [ngModel]="dataService.catalog.date | date:'yyyy-MM-dd'"
name="date" (ngModelChange)="date = $event" required [validateSuccess]="false"
placeholder="" data-error="This field is mandatory" class="form-control">
<label for="date">Date</label>
</div>
</div>
<div class="col-sm-12">
<div class="md-form form-sm">
<textarea type="text" id="description" [(ngModel)]="dataService.catalog.description" name="description"
value="{{ dataService.catalog.description }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="md-textarea form-control" mdbInputDirective rows="3"></textarea>
<label for="description">Description</label>
</div>
</div>
</div>
</form>


Button HTML (component.html):



<button mdbBtn type="button" color="success" class="waves-light" size="sm" mdbWavesEffect (click)="create()">
<i class="fa fa-plus-square mr-sm-2 mr-1"></i> Create Catalog
</button>


Create Method (component.ts):



create() {
//TODO: call the form's own validation method to show the erros?!

if(formValid) {
this.dataService.create(this.dataService.catalog).subscribe(value => {
console.log(value);
}, error => {
console.log(error);
});
}
}









share|improve this question

























  • can you show us something you've tried so that we can help ?

    – CruelEngine
    Jan 19 at 4:15











  • Updated my question with some code samples. The idea is to call the forms own validation methods and not create a custom validation mechanism.

    – Ricky
    Jan 19 at 10:33














1












1








1








I have a form but i'm using a button outside the form to submit the data to an API.
The form has error messages for required fields, and hey are shown then a user selects a input but doesn't fill in any data (default HTML5 validation).



Since i'm not actually submitting the form, is there a way to show the same validation messages when the user clicks the button outside the form?



UPDATE: Added some code to further explain the isse. Also, i forgot to mention that i'm using Material Design for Bootstrap as my CSS framework.



Form HTML (component.html):



<form class="custom-form p-2">
<div class="row">
<div class="col-sm-6">
<div class="md-form form-sm">
<input mdbInputDirective type="text" id="title" [(ngModel)]="dataService.catalog.title" name="title"
value="{{ dataService.catalog.title }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="form-control">
<label for="title">Title</label>
</div>
</div>
<div class="col-sm-2">
<div class="md-form form-sm">
<input mdbInputDirective type="date" id="date" [ngModel]="dataService.catalog.date | date:'yyyy-MM-dd'"
name="date" (ngModelChange)="date = $event" required [validateSuccess]="false"
placeholder="" data-error="This field is mandatory" class="form-control">
<label for="date">Date</label>
</div>
</div>
<div class="col-sm-12">
<div class="md-form form-sm">
<textarea type="text" id="description" [(ngModel)]="dataService.catalog.description" name="description"
value="{{ dataService.catalog.description }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="md-textarea form-control" mdbInputDirective rows="3"></textarea>
<label for="description">Description</label>
</div>
</div>
</div>
</form>


Button HTML (component.html):



<button mdbBtn type="button" color="success" class="waves-light" size="sm" mdbWavesEffect (click)="create()">
<i class="fa fa-plus-square mr-sm-2 mr-1"></i> Create Catalog
</button>


Create Method (component.ts):



create() {
//TODO: call the form's own validation method to show the erros?!

if(formValid) {
this.dataService.create(this.dataService.catalog).subscribe(value => {
console.log(value);
}, error => {
console.log(error);
});
}
}









share|improve this question
















I have a form but i'm using a button outside the form to submit the data to an API.
The form has error messages for required fields, and hey are shown then a user selects a input but doesn't fill in any data (default HTML5 validation).



Since i'm not actually submitting the form, is there a way to show the same validation messages when the user clicks the button outside the form?



UPDATE: Added some code to further explain the isse. Also, i forgot to mention that i'm using Material Design for Bootstrap as my CSS framework.



Form HTML (component.html):



<form class="custom-form p-2">
<div class="row">
<div class="col-sm-6">
<div class="md-form form-sm">
<input mdbInputDirective type="text" id="title" [(ngModel)]="dataService.catalog.title" name="title"
value="{{ dataService.catalog.title }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="form-control">
<label for="title">Title</label>
</div>
</div>
<div class="col-sm-2">
<div class="md-form form-sm">
<input mdbInputDirective type="date" id="date" [ngModel]="dataService.catalog.date | date:'yyyy-MM-dd'"
name="date" (ngModelChange)="date = $event" required [validateSuccess]="false"
placeholder="" data-error="This field is mandatory" class="form-control">
<label for="date">Date</label>
</div>
</div>
<div class="col-sm-12">
<div class="md-form form-sm">
<textarea type="text" id="description" [(ngModel)]="dataService.catalog.description" name="description"
value="{{ dataService.catalog.description }}" required [validateSuccess]="false" placeholder=""
data-error="This field is mandatory" class="md-textarea form-control" mdbInputDirective rows="3"></textarea>
<label for="description">Description</label>
</div>
</div>
</div>
</form>


Button HTML (component.html):



<button mdbBtn type="button" color="success" class="waves-light" size="sm" mdbWavesEffect (click)="create()">
<i class="fa fa-plus-square mr-sm-2 mr-1"></i> Create Catalog
</button>


Create Method (component.ts):



create() {
//TODO: call the form's own validation method to show the erros?!

if(formValid) {
this.dataService.create(this.dataService.catalog).subscribe(value => {
console.log(value);
}, error => {
console.log(error);
});
}
}






angular validation angular6 angular7 mdbootstrap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 10:30







Ricky

















asked Jan 18 at 23:41









RickyRicky

75321432




75321432













  • can you show us something you've tried so that we can help ?

    – CruelEngine
    Jan 19 at 4:15











  • Updated my question with some code samples. The idea is to call the forms own validation methods and not create a custom validation mechanism.

    – Ricky
    Jan 19 at 10:33



















  • can you show us something you've tried so that we can help ?

    – CruelEngine
    Jan 19 at 4:15











  • Updated my question with some code samples. The idea is to call the forms own validation methods and not create a custom validation mechanism.

    – Ricky
    Jan 19 at 10:33

















can you show us something you've tried so that we can help ?

– CruelEngine
Jan 19 at 4:15





can you show us something you've tried so that we can help ?

– CruelEngine
Jan 19 at 4:15













Updated my question with some code samples. The idea is to call the forms own validation methods and not create a custom validation mechanism.

– Ricky
Jan 19 at 10:33





Updated my question with some code samples. The idea is to call the forms own validation methods and not create a custom validation mechanism.

– Ricky
Jan 19 at 10:33












1 Answer
1






active

oldest

votes


















0














You can use your own functionality. Like store the value of each textbox and on clicking the submit button validate your values.






share|improve this answer
























  • Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

    – Ricky
    Jan 19 at 10:32











  • Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

    – Souradeep Panja
    Jan 19 at 15:04













  • I've tied that, but it doesn't work because i'm not using a valid ngForm.

    – Ricky
    Jan 19 at 16:11











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%2f54262787%2fangular-check-if-a-form-is-valid-from-outside-the-form-and-show-the-error-mess%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









0














You can use your own functionality. Like store the value of each textbox and on clicking the submit button validate your values.






share|improve this answer
























  • Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

    – Ricky
    Jan 19 at 10:32











  • Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

    – Souradeep Panja
    Jan 19 at 15:04













  • I've tied that, but it doesn't work because i'm not using a valid ngForm.

    – Ricky
    Jan 19 at 16:11
















0














You can use your own functionality. Like store the value of each textbox and on clicking the submit button validate your values.






share|improve this answer
























  • Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

    – Ricky
    Jan 19 at 10:32











  • Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

    – Souradeep Panja
    Jan 19 at 15:04













  • I've tied that, but it doesn't work because i'm not using a valid ngForm.

    – Ricky
    Jan 19 at 16:11














0












0








0







You can use your own functionality. Like store the value of each textbox and on clicking the submit button validate your values.






share|improve this answer













You can use your own functionality. Like store the value of each textbox and on clicking the submit button validate your values.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 5:06









Souradeep PanjaSouradeep Panja

1




1













  • Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

    – Ricky
    Jan 19 at 10:32











  • Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

    – Souradeep Panja
    Jan 19 at 15:04













  • I've tied that, but it doesn't work because i'm not using a valid ngForm.

    – Ricky
    Jan 19 at 16:11



















  • Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

    – Ricky
    Jan 19 at 10:32











  • Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

    – Souradeep Panja
    Jan 19 at 15:04













  • I've tied that, but it doesn't work because i'm not using a valid ngForm.

    – Ricky
    Jan 19 at 16:11

















Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

– Ricky
Jan 19 at 10:32





Hi! Yes, i know. But what i would like to know if there is a simpler way and just call the form's own validation methods to show the errors without creating a different validation mechanism.

– Ricky
Jan 19 at 10:32













Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

– Souradeep Panja
Jan 19 at 15:04







Then you can use like this 1)<formname>.ngSubmit.emit() 2)get form status with <formname>.form.valid <form (ngSubmit)="save()" #custForm="ngForm"> ... </form> <button class="btn-save button primary" (click)="custForm.ngSubmit.emit()" [disabled]="!custForm.form.valid">SAVE</button>

– Souradeep Panja
Jan 19 at 15:04















I've tied that, but it doesn't work because i'm not using a valid ngForm.

– Ricky
Jan 19 at 16:11





I've tied that, but it doesn't work because i'm not using a valid ngForm.

– Ricky
Jan 19 at 16:11


















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%2f54262787%2fangular-check-if-a-form-is-valid-from-outside-the-form-and-show-the-error-mess%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

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre