-1

Consider the following code: particularly when "none" is selected. Using sweetalerts2

Swal.fire({ //
    html: '
    blah blah ',

    input: 'select',
    inputOptions: {
      'none': 'Please select an option:',
      'option1': 'First Option',
      'option2': 'Second Option'
    },
    inputValidator: function(value) {
      return new Promise(function(resolve, reject) {
        if (value != 'none') {
          resolve();
        } else {
          reject('You need to select an option');
        }
      });
    }
  }).then(function(theoption) {
      // etc etc...

On selecting "none", there is a javascript error. On firefox console, for example, I get "uncaught exception: You need to select an option" I realise that I am probably la hopeless Newbee

Liam
  • 27,717
  • 28
  • 128
  • 190
Rajan
  • 15
  • 4

1 Answers1

1

When using sweetalert, you need to resolve() with the error message, not reject() the error message, thus, you need to change your reject(...) into:

resolve('You need to select an option');

See example below:

Swal.fire({
  html: 'blah blah ',

  input: 'select',
  inputOptions: {
    'none': 'Please select an option:',
    'option1': 'First Option',
    'option2': 'Second Option'
  },
  inputValidator: function(value) {
    return new Promise(function(resolve, reject) {
      if (value != 'none') {
        resolve();
      } else {
        resolve('You need to select an option');
      }
    });
  }
}).then(function(theoption) {
  if (theoption.value) {
    Swal.fire('Great!');
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

See documentation for further details.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64