I’ve created a form on WordPress-powered site. It’s currently using RequireJS to add two buttons to a page element, but I’d also like Require to control some things in the jQuery Validation plug-in. The adding of the buttons work fine, but sadly, the validation is not working.
The form looks like this in the HTML:
<form id="contact" method="get" action="">
<fieldset id="contact-form-fields">
<p>
<label for="form_name" class="form-titles">Name</label>
<input name="form_name" id="form_name" type="text" placeholder="Name" aria-required="true" class="required">
</p>
<p>
<label for="form_email" class="form-titles">Email</label>
<input name="form_email" id="form_email" type="text" placeholder="Email" aria-required="true" value="">
</p>
<p>
<label for="msg_text" class="form-titles">Email</label>
<textarea tabindex="-8" rows="10" placeholder="Type your awesome message here." class="placeholder" name="msg_text" id="msg_text" aria-required="true" class="required"></textarea>
</p>
</fieldset>
<input type="submit" value="Send Your Message" name="submit" class="button">
</form>
RequireJS is attached to the page like this:
<script data-main="http://livetest.kaidez.com/wp-content/themes/kaidez-2012/js/config.js" src="http://livetest.kaidez.com/wp-content/themes/kaidez-2012/js/require-jquery.js"></script>
The above-mentioned config.js file looks like this:
require.config({
baseUrl: "http://livetest.kaidez.com/wp-content/themes/kaidez-2012/js",
deps: ['scripts'],
paths: {
jquery: "jquery",
jqueryValidate: "jquery-validate.min"
},
shim: {
jquery: {
exports: "jquery"
},
jqueryValidate: {
deps: ["jquery"]
}
}
});
config.js says it has a dependency called scripts.js, which looks like this:
// code that adds buttons to the page
require(['jquery'], function ($) {
var header = document.getElementById("masthead"),
$navMenu = $("#site-navigation-list"),
$searchBox = $("#searchform"),
menuButton = document.createElement("div"),
searchButton = document.createElement("div"),
showMenus;
$(menuButton).attr("id", "menu");
$(searchButton).attr("id", "search");
header.appendChild(searchButton);
header.appendChild(menuButton);
showMenus = function(btn,el) {
$(btn).click(function() {
if (el.is(":visible") ) {
el.slideUp({
complete:function(){
$(this).css("display","");
}
});
} else {
el.slideDown();
}
});
};
showMenus(menuButton, $navMenu);
showMenus(searchButton, $searchBox);
});
//code that controls jQuery Validate
require(["jquery", "jqueryValidate"], function($, jqueryValidate) {
$("#contact").validate({
rules: {
form_email: {
required: true,
number: true
}
}
});
});
Looking at other posted questions, jsFiddles, etc and I can’t seem to find an answer. I think my error is in my RequireJS settings but I’m not sure. Working dev environment code is here. Any help is appreciated.
I believe that I’ve found an answer to my own question. The TL;DR answer is:
The long answer…here we go:
I was trying to use two RequireJS modules to manage form validation with some help from the jQuery Validation plugin (the above-mention code to add buttons doesn’t matter). I got this to work with the following code:
The contact form looks like this in the HTML:
RequireJS is referenced in the HTML like this:
config.js looks like this:
The above-mentioned form.js file looks like this:
And just for clarity, the PHP that sends out an email after a successful form submission (email.php) looks like this.
The good news is that everything above works as is. The bad news shows up if you use WordPress to install a jQuery-dependent plugin. Due to the WordPress architecture, plugins like this will automatically install jQuery core unless it’s already installed via
functions.php
or some other WP internal method. Calling jQuery via RequireJS like I did won’t stop this.I tried a lot of different methods to fix this using
wp_deregister_script()
,wp_enqueue_script()
, etc. but had no luck. This works fine for the personal project I’m working on but it’s probably not good for a client project.There is a WordPress ticket to add AMD functionality to WordPress with the goal of calling different versions of jQuery. (read it here). Hopefully this issue will be addressed if there’s follow-thru on the ticket. Until then, I’m pretty sure that this is how it is, for now.