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) »
  • Validate New Individual
Pages: [1]

Author Topic: Validate New Individual  (Read 660 times)

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Validate New Individual
July 22, 2012, 08:29:29 pm
How do I validate values being entered in the New Individual, or when a contact is edited?

I can get the validate hook to be called and validate the values, but when it returns to the edit screen there is no indication of the error messages that were sent unlike when using the validate hook with a profile.

Is there something different I need to do with the standard create/edit form for civicrm?

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: Validate New Individual
July 22, 2012, 08:37:50 pm

can you copy your validate hook code here so we can check

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

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Re: Validate New Individual
July 23, 2012, 01:35:36 pm
Here you go. It works fine on a custom profile.

Code: [Select]
function bpmccain_hooks_civicrm_validate( $formName, &$fields, &$files, &$form ) {
if ($formName == "CRM_Contact_Form_Contact"){
foreach($fields['phone'] as $key => $values) {
$checkPhone = check_phone($values['phone']);
if ( $checkPhone == FALSE) {
$errors['phone'][$key] = ts( 'Please enter a valid phone number' );
}
}
}
return empty( $errors ) ? true : $errors;
}

function check_phone($phone) {
$number = preg_replace('/[^\d]/', '', $phone);
if (strlen($number) == 10) {
return "(" . substr($number, 0, 3) . ") " . substr($number, 3, 3) . '-' . substr($number, 6);
}
else if ( (strlen($number) == 11) && (substr($number,0,1) == 1) ){
return "(" . substr($number, 1, 3) . ") " . substr($number, 4, 3) . '-' . substr($number, 7);
}
else { return false; }
}


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: Validate New Individual
July 23, 2012, 01:57:34 pm

whats the name of your module? is it: bpmccain_hooks.module?

if so, might want to add a debug statement and exit in the first line and check the formName for the edit form

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

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Re: Validate New Individual
July 24, 2012, 03:05:51 pm
I've already done that, and it shows up as 'CRM_Contact_Form_Contact'

I also know that the code is running, and that $checkPhone is evaluating as FALSE, as I have put an exit statement in there as a test.

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: Validate New Individual
July 24, 2012, 03:16:44 pm

try this instead:

Code: [Select]
  $errors["phone[$key]"] = ts( 'Please enter a valid phone number' );
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

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

This forum was archived on 2017-11-26.