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 (Moderator: Donald Lobo) »
  • Making standard contact fields required
Pages: [1]

Author Topic: Making standard contact fields required  (Read 1298 times)

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Making standard contact fields required
January 16, 2009, 10:40:49 am
Hi there,

I'm making some of the contact fields required and am very pround about the fact that I am using a hook to do this :-) following the hook validate example.  Here is my code:
Code: [Select]
function vawext_civicrm_validate( $formName, &$fields, &$files, &$form ) {
$errors = array( );
    if ( $formName == 'CRM_Contact_Form_Edit' ) {
       $streetAddress = CRM_Utils_Array::value( 'location_1_address_street_address', $fields );
       if ( ! $streetAddress ) {
          $errors['location_1_address_street_address'] = ts( 'Street address is a required field' );
       }
    }
    return empty( $errors ) ? true : $errors;
}

A couple of questions...

1) I'm not sure how to reference address fields in the contact form.  As you can see I want "location_1_address_street_address" to be validated, but this isn't the correct way to reference it - what is?

2) (feel free to tell me that i am being OTT but) Can I add a nice red * to show the user that the field is required in these cases?  Can I do that using hook_civicrm_buildForm?

Many thanks,
Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Making standard contact fields required
January 16, 2009, 12:22:08 pm
Quote
1) I'm not sure how to reference address fields in the contact form.  As you can see I want "location_1_address_street_address" to be validated, but this isn't the correct way to reference it - what is?

Use this:
$streetAddress = $fields['location'][1]['address']['street_address'];

Quote
2) (feel free to tell me that i am being OTT but) Can I add a nice red * to show the user that the field is required in these cases?  Can I do that using hook_civicrm_buildForm?
I guess easiest solution for this would be using Custom templates.

HTh

Kurund
Found this reply helpful? Support CiviCRM

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Making standard contact fields required
January 20, 2009, 08:30:21 am
Hi there - ok - simpler than i thought :) the only info I'm missing now is the correct way to reference the ['location'][1]['address']['street_address'] field in the $errors array so that I can send a properly formatted error message.  I thought $errors['location'][1]['address']['street_address'] was a good guess but it didn't work.

Cheers again,
Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Making standard contact fields required
January 20, 2009, 09:08:43 am
Can you try:

$errors['location[1][address][street_address]'] = ts( 'Street address is a required field' );

HTh

Kurund
Found this reply helpful? Support CiviCRM

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Making standard contact fields required

This forum was archived on 2017-11-26.