简体   繁体   中英

parsley.js - manually display a message without having to validate

I want to display a parsley message in an else clause of my javascript code:

if ( ...is valid ) {
    //do things
} else {
    //display parsley error
}

I know that Parsley allows custom validators as documented here: http://parsleyjs.org/documentation.html#javascript

But I merely want to display the message until the field is modified. I could create a validator such as:

$( '#myInput' ).parsley( {
    validators: {
      alwaysFalse: function ( val ) {
        return false;
      }
    }
  , messages: {
      myMessage: "Form is invalid"
    }
});

But then how would I trigger this and only this validator? (There is another validator already attached)

your messages object should be a mirror of your validators object but with the messages to display.

messages: {
  alwaysFalse: "Form is invalid"
}

and you could try

validators: {
  alwaysFalse: function(val){
    return false;
  },
  required: function ( val ) {
    return false;
  }
}

also

Warning : you must remove parsley-validate auto-binding code in your forms DOM to allow you to override the default processing and use Parsley purely from javascript.

it seems like what you really want this: http://parsleyjs.org/documentation.html#parsleyfield check out parsley-error-container

the trigger should be $( '#myInput' ).parsley( 'validate' );

or not 100% sure on this but you should be able to call each one like this:

$( '#myInput' ).parsley('alwaysFalse');

and if they need inputs or data:

$( '#myInput' ).parsley('alwaysFalse','inputs','data');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM