This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

grislysquirrel15554
grislysquirrel15554

Hello,

I'm currently writing a plugin and I don't know how to implement a way to show an alert when submitting a form without some fields (that are not required, but suggested).

I was thinking about editing in "create.htm" the "Save" button like this:

<button
                type="submit"
                data-request="onSave"
                data-request-data="redirect:0"
                data-load-indicator="Saving"
                <?php if(!field-check): ?>data-request-confirm="Do you really want to save? Some fields are empty."><?php endif ?>
                class="btn btn-primary">
                <u>S</u>ave
      </button>

Where "!field-check" is where it's checked if a specified field has been set.

Can you help me?

Thanks!

Last updated

philipptempel
philipptempel

Your code won't work because the if ( ! field-check) would be evaluated server-side when the form is being rendered. You will need to perform the check on form submission (with client-side code) or on the form processing (with server-side code asking for confirmation). My preferred way would be client-side validation. For this, please read the docs on AJAX data-attributes and especially on the AJAX JavaScript API. If you are struggling with implemented code causing nothing but errors, post them here.

grislysquirrel15554
grislysquirrel15554

Yes, but how can I access values from the form before they are saved?

philipptempel
philipptempel

Have a look here and see the 'ajaxBeforeSend' event. If you listen for this event on your form, you should be able to access it. Maybe you want to do something like

$('#yourFormId').on('ajaxBeforeSend', function(el) {
    console.log(el);
})

and see what is being logged to your console.

I am recommending this because I haven't done this yet with OctoberCMS, so I don't know how it works exactly. But from this point up there, it should be simple jQuery to perform your checks.

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.