Skip to content

Commit

Permalink
CRM-18981
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhbatra96 committed Jul 21, 2016
1 parent f3774cb commit 736c9b0
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
52 changes: 52 additions & 0 deletions api/v3/Generic/Validate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2016 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* @package CiviCRM_APIv3
*/

/**
* Provide meta-data for this api.
*
* @param array $params
*/
function _civicrm_api3_generic_validate_spec(&$params) {
$params['action']['api.required'] = TRUE;
}

/**
* Generic api wrapper used for validation of entity-action pair.
*
* @param array $apiRequest
*
* @return mixed
*/
function civicrm_api3_generic_validate($apiRequest) {
$errors = _civicrm_api3_validate($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']);

return civicrm_api3_create_success($errors, $apiRequest['params'], $apiRequest['entity'], 'validate');
}
78 changes: 78 additions & 0 deletions api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,84 @@ function _civicrm_api3_custom_data_get(&$returnArray, $checkPermission, $entity,
}
}

/**
* Used by the Validate API.
* @param string $entity
* @param string $action
* @param array $params
*
* @return array $errors
*/
function _civicrm_api3_validate($entity, $action, $params) {
$errors = array();
$fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => $action));

// Check for required fields here.

// Select only the fields which have been input as a param.
$fields = array_intersect_key($fields, $params);

// This derives heavily from the function "_civicrm_api3_validate_fields".
// However, the difference is that try-catch blocks are nested in the loop, making it
// possible for us to get all errors in one go.
foreach ($fields as $fieldName => $fieldInfo) {
switch (CRM_Utils_Array::value('type', $fieldInfo)) {
case CRM_Utils_Type::T_INT:
try {
_civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
break;

case CRM_Utils_Type::T_DATE:
case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
case CRM_Utils_Type::T_TIMESTAMP:
//field is of type date or datetime
try {
_civicrm_api3_validate_date($params, $fieldName, $fieldInfo);
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
break;

case CRM_Utils_Type::T_TEXT:
try {
_civicrm_api3_validate_html($params, $fieldName, $fieldInfo);
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
break;

case CRM_Utils_Type::T_STRING:
try {
_civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity);
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
break;

case CRM_Utils_Type::T_MONEY:
try {
list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
break;
}
foreach ((array) $fieldValue as $fieldvalue) {
if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) {
throw new Exception($fieldName . " is not a valid amount: " . $params[$fieldName]);
}
}
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
break;
}
}

return $errors;
}

/**
* Validate fields being passed into API.
*
Expand Down

0 comments on commit 736c9b0

Please sign in to comment.