Connect with us

Published on: 16 Apr 2018

How to create an order form in MODX with custom hook VAT check using formvalidation.io, FormIt and AjaxFrom.

The goal

A simple order form with a country select dropdown, if a EU country is selected make the VAT field mandatory using a custom FormIt post hook.

Add to this, check the VAT using the great plugin formvalidation.io.

Finally hide the VAT field is a non-EU country is selected.

Many thanks to: joeke, oetzienl, Joshua, markh and optimuscrime from the MODX Slack who helped me figure this out!

Dependencies

Install the following MODX plugins:

  • FormIt
  • AjaxForm

You will also need the following Javascript:

  • jQuery
  • formvalidation.io

The order_form_outer chunk


		

The order_form chunk


		

The order_form_email chunk


		

The btwNrCheck snippet

<?php
// Get country dropdown and set as variable
$country = $hook->getValue('country');
// If EU country is selected...
if (in_array($country, ['NL', 'AT', 'BE', 'BR', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IS', 'IE', 'IT', 'LV', 'LT', 'LU', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'RS', 'SK', 'SI', 'ZA', 'ES', 'SE', 'CH', 'GB', 'VE'])) {
	// Put VAT input field in variable
	$vat = $hook->getValue('vat');
	// If the VAT is empty, give error
	if (empty($vat)) {
		$hook->addError('vat', 'VAT can not be empty');    
		return false;        
	}
}
// If country not EU or VAT not empty set validation as true
return true;
?>

The order_form_js Javascript

You need jQuery for this to work.


	

Feel free to contact us to find out if we can help you improve your website.

Lets chat free of charge