a rough snipped to show the idea. vat fields only gets displayed if the selected country is a valid EU country and if the user has filled out company name. otherwise VAT field stays hidden.
That is not possible with the conditional_fields module, because addressfield has its own field structure.
(function ($) {
Drupal.behaviors.vat_number = {
attach:function (context, settings) {
//get initial page values
var company = $('.organisation-name').val();
var country = $('.country').val();
//console.log("country: " + country + " - company: " + company);
//valid eu countries, taken from drupal, ISO conform
var eu_countries = [
"AT",
"BE",
"BG",
"CY",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FR",
"GB",
"GR",
"HU",
"IE",
"IT",
"LT",
"LU",
"LV",
"MT",
"NL",
"PL",
"PT",
"RO",
"SE",
"SI",
"SK"
];
// var $valid_eu_country = jQuery.inArray(country, eu_countries);
// console.log("in array: " + $valid_eu_country);
//show the vat field if country is inside EU and company field has a value too
//get value of company field
$('.organisation-name').keyup(function () {
var company = $('.organisation-name').val();
//console.log(company);
//show form or hide it on changes of company field
if (((jQuery.inArray(country, eu_countries)) >= 0) && (company != '')) {
$('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').show();
}
else {
$('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').hide();
}
});
//get value of country select
$('.country').change(function () {
var country = $('.country').val();
//console.log(country);
});
//console.log("it is 1: " + jQuery.inArray($('.country').val(), eu_countries));
//console.log("it is 2: " + jQuery.inArray(country, eu_countries));
//show form or hide it.
if (((jQuery.inArray(country, eu_countries)) >= 0) && (company != '')) {
$('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').show();
}
else {
$('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').hide();
}
}
}
}(jQuery));
Comments
Comment #1
dwkitchen commentedComment #2
Darren Shelley commented+1
I would prefer an implementation based on Drupal 7's states API:
http://api.drupal.org/api/examples/form_example%21form_example_states.in...
Currently I use the following code in a custom module, this is easily extendible to encompass company name:
I recommend shifting this functionality into the main module if this flow is desired.
Comment #3
dwkitchen commentedGreat thanks Darren added to the Reverse Charge Module
Comment #5
adam1 commented#2 is added to the Reverse Charge Module? I can't find that rule. And when implementing #2 in a custom module, nothing happens and my VAT-field is not hidden if a country outside the EU is selected. Could someone tell, how I can accomplish the conditional displaying of the VAT number field?
Comment #6
mp commentedwould need this too for D7, any solution yet?
Comment #7
wizonesolutionsThis is already D7, and it already works. You have to make sure to turn on
commerce_eu_vat_rc.