3

I created two textboxes one for the title and another for the name.

I am using validations if the textboxes are not filled, so the information is only submitted if both are filled.

My problem is that after submitting I try to clear the values ​​of the variables and when clearing that value the validation messages appear.

Is there a way to successfully submit and clear the value of variables, the validator does not appear?

DEMO

html

<div style="margin-top:16px;width:50%">
    <dx-text-box placeholder="title..." [showClearButton]="true" [(ngModel)]="title">
        <dx-validator>
            <dxi-validation-rule type="required" message="Insert Title">
            </dxi-validation-rule>
        </dx-validator>
    </dx-text-box>
</div>
<div style="margin-top:16px;width:50%">
    <dx-text-box placeholder="name..." [showClearButton]="true" [(ngModel)]="name">
        <dx-validator>
            <dxi-validation-rule type="required" message="Insert Name">
            </dxi-validation-rule>
        </dx-validator>
    </dx-text-box>
</div>

<dx-button text="Submit" [useSubmitBehavior]="true" (onClick)="Save()"></dx-button>

.ts

title: string;
name: string;

Save(){
  if(this.title == "" || this.title == undefined || this.name == "" || this.name == undefined){
  }
  else{
    alert("Sucess !!");
    this.title = "";
    this.name = "";
  }
}

Problem

Image

Here, I filled in the textboxes and submitted successfully. I cleared the value of the variables, but when doing this, the validator is activated, when in fact everything was supposed to be in the initial state :(

S3ck Stros
  • 325
  • 3
  • 10

2 Answers2

1

here a trick I'm usually using to clear all validators after submit.

  @ViewChildren(DxValidatorComponent) validatorViewChildren: QueryList<DxValidatorComponent>;

  private clearDxValidators = () => {
    this.validatorViewChildren.toArray().map(ref => {
      ref.instance.reset();
    })
  }


read more about reset()

Muhammed Moussa
  • 4,589
  • 33
  • 27
  • I have the same problem, and this solution unfortunately does not solve the problem. If you use validation, after performing the reset, the validation message always appears. – John w. Jun 02 '20 at 21:44
  • @John , can you provide more detail about your validation handle and validation message, rest() should clear everything including messages. – Muhammed Moussa Jun 03 '20 at 00:02
0

As a quick workaround, you can wrap the template in a form, then use that to check if it's pristine, in order to know whether or not to show the validator message. Then instead of assigning an empty string to your properties, you can reset the form (after importing it in the component through ViewChild()).

That does feel like a hack though, because the toolkit you're using should automatically know this. If it doesn't, then why bother use it.

Also, I suggest you also start reading on Angular Reactive Forms.

Anyways, I attached my suggested changes in a Stackblitz.

Good luck with your project!

Dragos Andrei
  • 305
  • 1
  • 7
  • 1
    Many thanks for the reply. The cleaning of the variables is great, the only problem is that if you don't fill anything and click submit the validator doesn't appear to me :( – S3ck Stros May 29 '20 at 11:35
  • 1
    Ok, fixed it. *ngIf="form?.dirty" on both dxi-validation-rule, and then on the Save() function, if (...they are empty...) then this.form.form.markAsDirty(); and then the else. You can see my changes [also](https://stackblitz.com/edit/angular-pfuz9a) – Dragos Andrei May 29 '20 at 11:46
  • It's almost perfect. The only problem is that when I insert something in the title, clicking on the name's texbox immediately activates the validator. The validator is only supposed to appear in the submit if you do not fill in the required textboxes. – S3ck Stros May 29 '20 at 11:54
  • I'm sorry, but I'm not going to spend the time to make it perfectly usable for you. I only tried to show you one way to do it. There are multiple ways and, as I said, it is hacky because that's not normally how it's supposed to work. You should read up on the link that I have shared with you, and maybe try implementing it the Reactive way. Good luck! – Dragos Andrei May 29 '20 at 11:58