简体   繁体   中英

X-editable: close editing area on validation failure

What I have:

Multiple comments in some page. Each user can modify all his comments.

$.each(comments, function() {
    this.editable({
        type: 'textarea',
        mode: 'inline',
        onblur: 'submit',
        showbuttons: false,
        inputclass: 'edit-comments-text-input',
        validate: function(value) {
            if (!value.trim()) {
                return 'Can not be empty!'; //DON'T CLOSE THE EDITABLE AREA!!!
            }
        },
        success: function(response, newValue){        
            comment.text = newValue.trim();

    });
});

What a problem:

If user input an empty string, validation fails. BUT the editable area won't be closed, so user can move on and edit the next comment, with still open editing area for the previous.

Question:

How to close editing text area on validation failure?

I read some source code, and I got it sorted.

The important line is:

$('.editable-open').editableContainer('hide', 'cancel');

so in the example above this would be:

$.each(comments, function() {
    this.editable({
        type: 'textarea',
        mode: 'inline',
        onblur: 'submit',
        showbuttons: false,
        inputclass: 'edit-comments-text-input',
        validate: function(value) {
            if (!value.trim()) {
                $('.editable-open').editableContainer('hide', 'cancel');
                return 'Can not be empty!'; //DOES NOW CLOSE THE EDITABLE AREA!!!
            }
        },
        success: function(response, newValue){        
            comment.text = newValue.trim();

    });
});

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