Yapapaya / jquery-cloneya

A jquery plugin to clone DOM elements
MIT License
76 stars 28 forks source link

Validation method #43

Open gchokeen opened 9 years ago

gchokeen commented 9 years ago

Form validation is taking important role in all the applications. So I think cloneya should give the control to user for validating form.

Case:

I don't see anyway to achieve this on before_clone or before_delete event callback.

Can return false do the trick ?

gchokeen commented 9 years ago

I did some work around to achieve above request, please check and tell me this can be implemented.

$(function () {

    var clone = $("#clone");
    clone.click(function () {

        alert("clicked");

        var e = $.Event("before_clone.cloneya");
        $(this).triggerHandler(e);

        if (e.isDefaultPrevented()) {
            return; 
        }

        alert("cloned");
    });

    clone.on("before_clone.cloneya", function (e) {

        if(!confirm('Are you sure, you want to clone this?')){
            e.preventDefault();
        }
    });

});

I created the js fiddle demo here

actual-saurabh commented 8 years ago

This is fine, but you'd also need to write the unit tests for validation. Let me know if you need help with that.

actual-saurabh commented 8 years ago

I'm keeping it for a future 1.1.0 release. I need to write more tests for nested cloning and deleting. This would've saved me #41

actual-saurabh commented 8 years ago

@gchokeen I wanted to discuss the flow. For example, when cloning the following events can be prevented

  1. before_clone.cloneya
  2. after_clone.cloneya
  3. before_append.cloneya
  4. after_append.cloneya

So, if 1 is prevented, 2, 3 & 4 would also need to be prevented. Same way, if 2 is prevented, 3 and 4 would also need prevention. I want to avoid too many nested if statements.

gchokeen commented 8 years ago

@actual-saurabh, I created validation branch to implement this feature. I will implement and update soon.

I think once before_clone.cloneya event prevented all other follow up event's should not trigger. Since we return if event prevented.

actual-saurabh commented 8 years ago

👍