For a order form I want to check is someone has filled in a correct VAT number. I found a site and some code to check this. The site returns a true of a false.
The code I have written, never gives back a 'false' but I can't find out why. Can someome see what is wrong with it? The landcode is the first 2 characters of the filled in 'vatnumber'.
Is there BTW a way to 'debug' in Chronoforms? So that I can see what values the variables get during the process?
The code I have written, never gives back a 'false' but I can't find out why. Can someome see what is wrong with it? The landcode is the first 2 characters of the filled in 'vatnumber'.
<?php
$vatnumber = $form->data['vatnumber'];
$land = substr($vatnumber, 0, 2);
$btwnummer = substr($vatnumber, 2);
$result = file_get_contents("http://isvat.appspot.com/".$land."/".$btwnummer."/", "r");
if($result == "false") {
$form->validation_errors['vatnumber'] = "Sorry, the VATnumber is invalid. Please try again.";
return false;
}
?>
Is there BTW a way to 'debug' in Chronoforms? So that I can see what values the variables get during the process?
Hi InternEd,
The code looks OK to me. Here's a slightly modified version that seems to work OK on the 'real' UK and French VAT numbers I've been able to find:
This has some extra code to clean the string and remove spaces, dashes and slashes before sending.
There's also some debug code there commented out as an example. You can also drag a Debugger action into the On Submit event to get more if you need it.
Bob
The code looks OK to me. Here's a slightly modified version that seems to work OK on the 'real' UK and French VAT numbers I've been able to find:
<?php
$vatnumber = $form->data['vatnumber'];
$vatnumber = trim($vatnumber);
$vatnumber = str_replace(array(' ', '-', '//'), '', $vatnumber);
$land = substr($vatnumber, 0, 2);
$btwnummer = substr($vatnumber, 2);
$url = "http://isvat.appspot.com/".$land."/".$btwnummer."/";
// $mainframe =& JFactory::getApplication();
// $mainframe->enqueuemessage('$url: '.print_r($url, true).'<hr />');
$result = file_get_contents($url, "r");
// $mainframe->enqueuemessage('$result: '.print_r($result, true).'<hr />');
if ( $result == "false" ) {
$form->validation_errors['vatnumber'] = "Sorry, the VATnumber is invalid. Please try again.";
return false;
}
?>This has some extra code to clean the string and remove spaces, dashes and slashes before sending.
There's also some debug code there commented out as an example. You can also drag a Debugger action into the On Submit event to get more if you need it.
Bob
Bob, Thanks for the code!
Strange thing is, that no matter what I fill in in the VATnumber field on the form, it always validates.
Any idea what that can be?
Regards, Ed.
Strange thing is, that no matter what I fill in in the VATnumber field on the form, it always validates.
Any idea what that can be?
Regards, Ed.
Hi InterEd,
You don't have an Event Loop action in the On Fail event to re-load the form???
Bob
You don't have an Event Loop action in the On Fail event to re-load the form???
Bob
Hi,
Yes I do. In the validation check there are already 2 other checks on submit.
Those 2 work, but when I add this one, it doesn't work.
To test, I did remove the other 2, to leave only this one in, but same result.
Thinking of that, does the field have to be set to 'required' to be able to do validation?
Ed.
Yes I do. In the validation check there are already 2 other checks on submit.
Those 2 work, but when I add this one, it doesn't work.
To test, I did remove the other 2, to leave only this one in, but same result.
Thinking of that, does the field have to be set to 'required' to be able to do validation?
Ed.
Hi InterEd,
Please copy and paste all the validation code here so I can take a look.
Bob
Please copy and paste all the validation code here so I can take a look.
Bob
Hi,
I deleted one of the two existing validations checks. That one wasn't necessary after all.
The code in the validation item now is this:
The first check (on the email address) works fine.
Thanks for checking!
Ed.
I deleted one of the two existing validations checks. That one wasn't necessary after all.
The code in the validation item now is this:
<?php
$email = $form->data['email'];
if ( !$email ) {
return true;
}
$email = explode('@', $email);
$domain = explode('.', $email[1]);
$domain = $domain[0];
if ( in_array( $domain, array('gmail', 'live', 'hotmail', 'aol', 'yahoo', 'zonnet', 'planet')) ) {
$form->validation_errors['email'] = "Sorry, the used email domain is invalid. Please use your corporate email address.";
return false;
}
?>
<?php
$vatnumber = $form->data['vatnumber'];
$vatnumber = trim($vatnumber);
$vatnumber = str_replace(array(' ', '-', '//'), '', $vatnumber);
$land = substr($vatnumber, 0, 2);
$btwnummer = substr($vatnumber, 2);
$url = "http://isvat.appspot.com/".$land."/".$btwnummer."/";
// $mainframe =& JFactory::getApplication();
// $mainframe->enqueuemessage('$url: '.print_r($url, true).'<hr />');
$result = file_get_contents($url, "r");
// $mainframe->enqueuemessage('$result: '.print_r($result, true).'<hr />');
if ( $result == "false" ) {
$form->validation_errors['vatnumber'] = "Sorry, the VATnumber is invalid. Please try again.";
return false;
}
?>
<?php
/**
* dynamic subject met vaste tekst
*/
$company = $form->data['company'];
$subject = "License Request from $company";
$form->data['subject'] = $subject;
?>
The first check (on the email address) works fine.
Thanks for checking!
Ed.
Hi InternEd,
I think that the problem is that the return true; in the first part of the code is ending the validation before the VAT check. Here's an amended version of that part of the code which uses $isvalid to allow you to run multiple validation checks before returning to the form:
Bob
I think that the problem is that the return true; in the first part of the code is ending the validation before the VAT check. Here's an amended version of that part of the code which uses $isvalid to allow you to run multiple validation checks before returning to the form:
<?php
$isvalid = true;
$email = $form->data['email'];
if ( $email ) {
$email = explode('@', $email);
$domain = explode('.', $email[1]);
$domain = $domain[0];
if ( in_array( $domain, array('gmail', 'live', 'hotmail', 'aol', 'yahoo', 'zonnet', 'planet')) ) {
$form->validation_errors['email'] = "Sorry, the used email domain is invalid. Please use your corporate email address.";
$isvalid = false;
}
}
$vatnumber = $form->data['vatnumber'];
$vatnumber = trim($vatnumber);
$vatnumber = str_replace(array(' ', '-', '//'), '', $vatnumber);
$land = substr($vatnumber, 0, 2);
$btwnummer = substr($vatnumber, 2);
$url = "http://isvat.appspot.com/".$land."/".$btwnummer."/";
$mainframe =& JFactory::getApplication();
$mainframe->enqueuemessage('$url: '.print_r($url, true).'<hr />');
$result = file_get_contents($url, "r");
$mainframe->enqueuemessage('$result: '.print_r($result, true).'<hr />');
if ( $result == "false" ) {
$form->validation_errors['vatnumber'] = "Sorry, the VAT number is invalid. Please try again.";
$isvalid = false;
}
if ( !$isvalid ) {
return false;
}
?>Bob
Bob, I don't know what happens, but I still get strange results.
When I apply a good email address, it still doesn't matter if I fill in a wrong or a right VAT number. The form just validates.
When I apply a wrong email address and a good VAT number, I get a message on the email address and at the top of the form I see this:

When I apply a wrong email address and a wrong VAT number, I get a message on the email address and at the top of the form I see this:
When I apply a good email address, it still doesn't matter if I fill in a wrong or a right VAT number. The form just validates.
When I apply a wrong email address and a good VAT number, I get a message on the email address and at the top of the form I see this:

When I apply a wrong email address and a wrong VAT number, I get a message on the email address and at the top of the form I see this:
Hi InterEd,
Hmmm it looks as though you aren't connecting to the appspot.com site. If I try the same Vat Number I get this:[attachment=0]04-05-2012 14-35-37.png[/attachment]
What happens if you just type the url into your browser (or click this link)?
http://isvat.appspot.com/nl/095999462B01/
Bob
Hmmm it looks as though you aren't connecting to the appspot.com site. If I try the same Vat Number I get this:[attachment=0]04-05-2012 14-35-37.png[/attachment]
What happens if you just type the url into your browser (or click this link)?
http://isvat.appspot.com/nl/095999462B01/
Bob
If I do it in my browser, I do get a true (or a false if I try a wrong number)
Hmmmmmm, maybe it's a dns problem on the server this website runs on.
Hmmmmmm, maybe it's a dns problem on the server this website runs on.
Hi InternEd,
There's a note in the PHP Manual for file_get_contents() - not sure if it helps:
Bob
There's a note in the PHP Manual for file_get_contents() - not sure if it helps:
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
Bob
Bob,
I copied the form to another website that runs by another provider, and there it works!
So I guess it indeed is a DNS problem of some sort. I will contact that provider.
I copied the form to another website that runs by another provider, and there it works!
So I guess it indeed is a DNS problem of some sort. I will contact that provider.
When I check the php info on both sites, I do see a difference in this parameter: allow_url_fopen
On one site it is set to ON on the 'problemsite' it is set to OFF.
So I guess that when I ask the provider if they can change this parameter, it will work.
On one site it is set to ON on the 'problemsite' it is set to OFF.
So I guess that when I ask the provider if they can change this parameter, it will work.
Bob,
The issue is solved. It turned out that the redirection in the code was blocked by the host of the website. They opened the ip-adresses in the firewall and now it works.
Thanks for the support. Let me buy you a beer 😀
Ed
The issue is solved. It turned out that the redirection in the code was blocked by the host of the website. They opened the ip-adresses in the firewall and now it works.
Thanks for the support. Let me buy you a beer 😀
Ed
This topic is locked and no more replies can be posted.
