• Resolved leost

    (@leost)


    I am trying to set a field to required and just realized that WordPress ignores the default html field required functionality. Then I found the snippet to resolve this: https://github.com/CMB2/CMB2-Snippet-Library/blob/master/javascript/cmb2-js-validation-required.php.

    I am using CMB2 from inside a small plugin I created. It’s working fine. So I put the snippet file inside my plugin folder, together with the cmb2-functions.php file, and included the cmb2-js-validation-required.php into cmb2-functions.php. Checked the HTML source, and saw the jquery code there. The attribute data-validation=”required” is added correclty to the field. All seem to be fine. But the required still not working.

    What am I doing wrong?

    There is a line in the snippet that gets the form element by id: $form = $( document.getElementById( 'post' ) );. There is no element with the id ‘post’. Should I change this id to something else? Or the problem lies somewhere else?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Have you set any of the fields to have the data-validation attribute yet? aka data-validation="required"

    https://github.com/CMB2/CMB2/wiki/Field-Parameters#attributes for some examples of how to potentially add your own.

    Regarding the #post ID attribute, that looks like it’s specific to classic editor/non-gutenberg so if you’re using this all in a Gutenberg based environment then the script needs to be updated appropriately.

    Thread Starter leost

    (@leost)

    You were right, the problem was I was using Gutenberg editor. Just installed the classic editor and it worked.

    Since I don’t use any Gutenber stuff, I will just stick to classic editor. I bet updating the script to work with Gutenberg would not be a simple task.

    Thank you!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Required field’ is closed to new replies.