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) »
  • API field verify
Pages: [1]

Author Topic: API field verify  (Read 874 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
API field verify
January 11, 2011, 01:32:57 am
Hi,

Just talked with X about the 'verify mandatory' when we want to ensure x or y is present.

I modified civicrm_verify_mandatory (v3) in the api branch http://svn.civicrm.org/civicrm/branches/trunk.api/api/v3 so that it accepts

$keys = array('id',
                     array('contact_id', 'contact_b'));

to denote that required fields are 'id' AND ('contact_id' OR 'contact_b')

There is a wrapper

function civicrm_verify_one_mandatory

that apparently makes it easier if you are just passing 'x' or 'y'

(X said it would & it seemed easy enough to do)

Note that for GET functions the $daoName should be left as null
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API field verify
January 11, 2011, 01:41:10 am
Hi,

So if you want civicrm_foo_bar to have either and id OR a name, you can

try {
    civicrm_verify_one_mandatory ($params,array ('id','name'));

or (without the helper, less readable IMO)
    civicrm_verify_mandatory ($params,array(array ('id','name')));


If you need both the id AND the name:
    civicrm_verify_mandatory ($params,array ('id','name'));


X+


-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API field verify
January 11, 2011, 01:45:02 am
So, the function now starts

Code: [Select]
function civicrm_tag_get($params)
{   
   try {

    civicrm_verify_one_mandatory (&$params, null, array('id', 'name')); 
    _civicrm_initialize( );

Can we leave     _civicrm_initialize( ); until after the verification? If so it would be more efficient. Seems to work

Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API field verify
January 11, 2011, 01:51:38 am
And another one .. is it ever valid to pass in an empty $params array - guess not if version is required. We should probably check for version in verify_mandatory for 3.x
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API field verify
January 11, 2011, 02:15:33 am
Quote from: Eileen on January 11, 2011, 01:45:02 am
Can we leave     _civicrm_initialize( ); until after the verification? If so it would be more efficient. Seems to work

I'd argue that optimising the failure path (that shouldn't be taken anyway) isn't the path we should worry about performance.

Better leave it at the top, later we might need it within the verify_mandatory for whatever wicked reason that will come up (eg. that the mandatory params depend on the user access right...).

If calling the api with a wrong param takes a few ms more than the optimal solution ot return an error, so be it.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API field verify
January 11, 2011, 11:22:57 am
Agree with Xavier and good addition Eileen!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API field verify
January 11, 2011, 02:55:22 pm
OK - cool - I notice a comment from Coleman on the wiki page about us needing to make sure civicrm_initialize is in all functions - so will do it as I see it
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

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

This forum was archived on 2017-11-26.