CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Validating postcodes
Pages: [1]

Author Topic: Validating postcodes  (Read 1462 times)

idmacdonald

  • I post occasionally
  • **
  • Posts: 69
  • Karma: 1
Validating postcodes
November 24, 2009, 06:59:09 am
Hi,

I'm trying to use hook_civicrm_validate to validate UK postcodes that are entered for contacts. So far I have a function that examines any postcodes submitted with the form and blocks the form submission if the postcode is not in the correct format. However, I can't get an error message to be displayed.

The documentation at http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification doesn't give many details regarding the return array. I've tried the following versions:
Code: [Select]
$errors['address_1_postal_code'] = ts('Postcode is not a valid UK postcode');
$errors['address'][1]['postal_code'] = ts('Postcode is not a valid UK postcode');

Both will block submission of the form, but neither cause the error message to be displayed (or highlight the postcode field).
Here's my function:

Code: [Select]
function mymodule_civicrm_validate( $formName, &$fields, &$files, &$form ) {
  $errors = array();
  if ($formName == 'CRM_Contact_Form_Contact') {
    foreach ($fields['address'] as $address) {
      if (empty($address['country_id']) || $address['country_id'] == 1226) {
$postcode = str_replace(' ', '', strtoupper($address['postal_code']));
$preg = "/^([A-PR-UWYZ]([0-9]([0-9]|[A-HJKSTUW])?|[A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)[0-9][ABD-HJLNP-UW-Z]{2}|GIR0AA)$/";
$match = preg_match($preg, $postcode) ? true : false;
if (!$match) {
  $errors['address_1_postal_code'] = ts('Postcode is not a valid UK postcode');
}
      }
    }
  }
  return empty($errors) ? true : $errors;
}

Can anyone tell me what the return array should look like?

Also, since the $fields variable is passed in by reference, I thought maybe I could alter the field's value here. But I tried and it doesn't seem to work. I just want to perform a strtoupper() on the postcode itself before saving it in the database. I guess I should use hook_civicrm_pre() to do this? I'm using CiviCRM version 3.1.alpha1.

Best wishes,
-Ian

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Validating postcodes
November 24, 2009, 07:56:34 am

can u try this

$errors['address[1][postal_code]'] = ts('Postcode is not a valid UK postcode');

for your second question, hook_civicrm_pre is probably the better choice

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

idmacdonald

  • I post occasionally
  • **
  • Posts: 69
  • Karma: 1
Re: Validating postcodes
November 24, 2009, 09:20:21 am
That did the trick. I've added my code as an example to the CiviCRM hook documentation.

Thanks very much.

-Ian

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Validating postcodes

This forum was archived on 2017-11-26.