Jump to content

Require customers to select at least one option


gjkathome

Recommended Posts

I am using CubeCart to collect reservations for a meal. Customers can choose starters (£7), mains (£16) and desserts (£7). The venue requires that the minimum order is a main course + one other. Currently I have set the product price to zero and the option prices to £7 or £16 as appropriate. The mains options are required, starter and dessert options are not. That gets the pricing right but doesn't enforce the minimum order rule. Is there any way I can fix this?

Many thanks

Graham

Edited by gjkathome
Link to comment
Share on other sites

Here is test code to show how Foundation's feature to "validate" form entry data can be used to solve this situation.

It is meant to be a separate page request (like, www.store.com/validate.html), but loads two files from the Foundation skin. (So, be sure to have the Foundation skin in CubeCart's /skins/ folder.)

The essential part, the "#test" validate object, would be incorporated in the overall collection of validate objects (located in Foundation's javascript file "3.cubecart.validate.js").

<!DOCTYPE html>
<html>
	<head>
		<script src="skins/foundation/js/vendor/jquery.js"></script>
	</head>
	<body>
		<form id="test">
		<select name="main" id="main">
			<option value="">Select</option>
			<option value="Steak">Steak</option>
			<option value="Chicken">Chicken</option>
			<option value="Fish">Fish</option>
			<option value="Tofu">Tofu</option>
		</select>
		<select name="entry" id="entry">
			<option value="">Select</option>
			<option value="Cheese">Cheese</option>
			<option value="Wings">Wings</option>
			<option value="Rings">Rings</option>
			<option value="Chili">Chili</option>
		</select>
		<select name="dessert" id="dessert">
			<option value="">Select</option>
			<option value="Cake">Cake</option>
			<option value="Pie">Pie</option>
			<option value="Sorbet">Sorbet</option>
			<option value="CheeseCake">CheeseCake</option>
		</select>
	<br><input id="submit" type="submit" value="Submit" />
		</form>
		<script src="skins/foundation/js/vendor/jquery.validate.js"></script>
		<script>function init_add_to_basket(){return false}</script>
		<script>
jQuery(document).ready(function() {
    $.validator.setDefaults({
        errorElement: 'small',
        errorPlacement: function(error, element) {
				if (element.attr("name")=="entry" || element.attr("name")=="dessert") {
					error.insertAfter("#dessert");
				} else 
            if (element.is(":radio") || element.is(":checkbox")) {
                var errorLocation = element.attr('rel');
                if ($('#' + errorLocation).length) {
                    error.insertAfter('#' + errorLocation);
                } else {
                    element.removeClass("error");
                    alert(error.text());
                }
            } else {
                error.insertAfter(element);
            }
        }
    });

    $("#test").validate({
			rules: {
				main:   { required:true },
				entry:  { required:{depends:function(e){return $("#main option:selected").val().length>0 && $("#dessert option:selected").val().length==0;}} },
				dessert:{ required:{depends:function(e){return $("#main option:selected").val().length>0 && $("#entry   option:selected").val().length==0;}} }
			},
			groups: {
				withName: "entry dessert"
			},
			messages: {
				main: { required: "You must choose a main dish." },
				entry: { required: "You must choose either an entry or a dessert, or one of both." },
				dessert: { required: "You must choose either an entry or a dessert, or one of both." }
			},
		});
		
    /* Reset Form */
    $('input:reset').click(function() {
        $(this).parents('form:first').validate().resetForm();
    });
});
		</script>
	</body>
</html>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...