Bhagyashri
Bhagyashri

Reputation: 182

alert controller setmessage not working in ionic 4

In alertController I want to call prompt.setMessage() if the input field is empty .But this is not valid function in ionic4

code

 const prompt= await this.alertController.create({
      header: 'insert text',
      message: 'enter text',
      inputs: [
        {
          name: 'itemtext',
          placeholder: 'enter text'
        }
      ],
      buttons: [{
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Ok',
          handler: async (data:any) => {
            if(data.itemtext==""){
              prompt.setMessage("text should not be empty");
              return false;
            }
            else{
            console.log("data.itemtext");
            }
          }
        }
      ]
    });
    await prompt.present();

and I don't want to close alert promt if text is empty please help.

Upvotes: 2

Views: 1322

Answers (1)

Sergey Rudenko
Sergey Rudenko

Reputation: 9235

You no longer have the method in Ionic 4, but you could still directly change the message property to achieve what you want:

const prompt= await this.alertController.create({
      header: 'insert text',
      message: 'enter text',
      inputs: [
        {
          name: 'itemtext',
          placeholder: 'enter text'
        }
      ],
      buttons: [{
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Ok',
          handler: (data:any) => {
            if(data.itemtext==""){
              prompt.message = "text should not be empty";
              return false;
            }
            else{
              console.log(data.itemtext);
            }
          }
        }
      ]
    });
    await prompt.present();

I also removed "async" from your method in the handler as you do not need it there.

Upvotes: 5

Related Questions