2

I've created a custom fieldtype... which is basically a button that triggers an AJAX action. When I add this field to my section, I can click the button while I'm editing an entry to perform an action.

Here's the thing... How can I tell if the entry needs to be saved? Clearly Craft is aware of whether an entry is unsaved or not, because it'll warn you before navigating away from the page. Is it possible for my button to tap into that same behavior?

MORE INFO:

This button does not take people away from the page, so I'm not sure if data-confirm-unload really comes into play here. If the entry is unsaved, the button will be replaced with a message saying, "you must first save the entry".

  • Should be able to add 'data-confirm-unload' to your form tag and Craft should pick it up? – Luke Holder Nov 12 '15 at 6:22
  • is gets used around here: github.com/pixelandtonic/Craft-Release/blob/… might be able to see how it uses window's beforeunload js event – Luke Holder Nov 12 '15 at 6:24
  • Thanks @LukeHolder. I'm not really concerned with folks navigating away... I want the button to only appear if the entry is saved (and show a "must save" message otherwise). It's a standard entry page, so I believe the form already contains a data-confirm-unload attribute. Is that still the right direction? – Lindsey D Nov 12 '15 at 6:27
  • you couldn't just see if the field is on an element with an ->id ? – Luke Holder Nov 16 '15 at 4:30
  • @LukeHolder: Interesting idea! However, I also need to know if the entry has changed... not just if it's been saved the first time. – Lindsey D Nov 16 '15 at 5:09
3

Craft checks if a form has changed using javascript in the cp.js file, see line 196.

You could replicate this quite easily yourself though, here is a stripped down example:

var initialFormValues = $('form[data-confirm-unload]').serialize();

function checkFormChanged() {
    if ($('form[data-confirm-unload]').serialize() != initialFormValues) {
        return true;
    }

    return false;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.