0

Ich verwende die formValidation plugin, die sich hier befindet, um eine Eingabeüberprüfung für ein HTML5-Formular durchzuführen. Die Fehlerüberprüfung funktioniert gut, aber jetzt habe ich einen Fall, wo ich eine Warnung anstelle von Fehler auf eine Eingabeüberprüfung bereitstellen muss, wo der Benutzer weiterhin mit einer Übermittlung fortfahren kann, wenn ungültig.Wie validiere ich mit "warning" anstatt "error" mit formValidation plugin?

Ich habe eine schnelle Suche nach dem Thema gemacht, aber nichts lohnenswertes gefunden, weshalb ich hier für einen Ratschlag poste.

Frage:

Wie kann ich mit einer Warnung über das formValidation Plugin validieren?

HTML-Eingang -

<input id="ApplicationID" name="Application" required="required" type="text" class="form-control"> 

-Code -

 //Validate the required input fields to prevent submit if not 
    //populated. 
    $('#createForm').formValidation({ 
     framework: 'bootstrap', 
     icon: { 
      valid: 'glyphicon glyphicon-ok', 
      invalid: 'glyphicon glyphicon-remove', 
      validating: 'glyphicon glyphicon-refresh' 
     }, 
     fields: { 

      Application: { 
       validators: { 
        notEmpty: { 
         message: 'The idVal name field is required' 
        }, 
        callback: { 
         message: 'A record for this Application type already exists!', 
         callback: function (value, validator, $field) { 
          // Determine if the input idVal already exists 
          var idVal = $('#ApplicationID').val(); 

          //Check that idVal isn't empty before calling match validation 
          if(idVal) 
          { 
           //Call a bool method to check if idVal already exists 
           return checkEPRIDExists(idVal); 
          } 

         } 

        } 
       } 
      } 

     } 


    }); 

Die aktuelle Validierung verhindert vorlegen und Highlights rot, auf der Suche nach einer gelben Warn statt, die, wenn ungültige erlaubt einreichen:

enter image description here

Antwort

0

Ich bin mir nicht sicher, wie du mit dem hier angefangen hast, seit es vor einer Weile war.

Hier ist ein Beispiel, das beschreibt, wie Sie eine Warnung für ein Feld implementieren (anstatt nur einen Fehler oder Erfolg).

Ich denke, das Wichtigste ist, dass Sie die Ereignisse hinzufügen müssen entweder success oder error innerhalb FormValidation auszulösen.

$('#createForm').formValidation({ 
    framework: 'bootstrap', 
    icon: { 
     valid: 'glyphicon glyphicon-ok', 
     invalid: 'glyphicon glyphicon-remove', 
     validating: 'glyphicon glyphicon-refresh' 
    }, 
    fields: { 
     Application: { 
      validators: { 
       notEmpty: { 
        message: 'The idVal name field is required' 
       }, 
       callback: { 
        message: 'A record for this Application type already exists!', 
        callback: function (value, validator, $field) { 
         // Determine if the input idVal already exists 
         var idVal = $('#ApplicationID').val(); 

         //Check that idVal isn't empty before calling match validation 
         if(idVal) 
         { 
          //Call a bool method to check if idVal already exists 
          return checkEPRIDExists(idVal); 
         } 

        } 

       } 
      } 
     } 

    } 
}) 
// This event will be triggered when the field passes given validator 
.on('success.validator.fv', function(e, data) { 
    // data.field  --> The field name 
    // data.element --> The field element 
    // data.result --> The result returned by the validator 
    // data.validator --> The validator name 

    if (data.field === 'Application' 
     && data.validator === 'callback' 
     && (data.fv.isValidField('Application'))){ 
     // The Application field passes the callback validator 
     data.element     // Get the field element 
      .closest('.form-group')  // Get the field parent 

      // Add has-warning class 
      .removeClass('has-success') 
      .addClass('has-warning') 

      // Show message 
      .find('small[data-fv-validator="callback"][data-fv-for="Application"]') 
       .show(); 
    } 
}) 
// This event will be triggered when the field doesn't pass given validator 
.on('err.validator.fv', function(e, data) { 
    // Removes the has-warning class when it doesn't pass any validator 
    if (data.field === 'Application') { 
     data.element 
      .closest('.form-group') 
      .removeClass('has-warning'); 
    } 
});