*/
function _civicrm_api3_generic_validate_spec(&$params) {
$params['action']['api.required'] = TRUE;
+ $params['action']['title'] = ts('API Action');
}
/**
$fields = $fields['values'];
// Check for required fields.
- foreach ($fields as $field => $values) {
- if (!empty($values['api.required'] && empty($params[$field]))) {
- $errors[$values['name']] = "Mandatory key(s) missing from params array: " . $values['name'];
+ foreach ($fields as $values) {
+ if (!empty($values['api.required']) && empty($params[$values['name']])) {
+ $errors[$values['name']] = array(
+ 'message' => "Mandatory key(s) missing from params array: " . $values['name'],
+ 'code' => "mandatory_missing",
+ );
}
}
// Select only the fields which have been input as a param.
- $fields = array_intersect_key($fields, $params);
+ $finalfields = array();
+ foreach ($fields as $values) {
+ if (array_key_exists($values['name'], $params)) {
+ $finalfields[] = $values;
+ }
+ }
// 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 $fieldInfo) {
+ foreach ($finalfields as $fieldInfo) {
+ $fieldName = $fieldInfo['name'];
try {
- _civicrm_api3_validate_switch_cases($fieldInfo, $entity, $params);
+ _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params);
}
catch (Exception $e) {
- $errors[$fieldName] = $e->getMessage();
+ $errors[$fieldName] = array(
+ 'message' => $e->getMessage(),
+ 'code' => 'incorrect_value',
+ );
}
}
*
* @throws Exception
*/
-function _civicrm_api3_validate_switch_cases($fieldInfo, $entity, $params) {
- $feildName = $fieldInfo['name'];
+function _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params) {
switch (CRM_Utils_Array::value('type', $fieldInfo)) {
case CRM_Utils_Type::T_INT:
_civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
case CRM_Utils_Type::T_MONEY:
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]);
--- /dev/null
+<?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 |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Tests for the generic validate API action.
+ *
+ * @package CiviCRM_APIv3
+ * @group headless
+ */
+class api_v3_ValidateTest extends CiviUnitTestCase {
+ /**
+ * This method is called before a test is executed.
+ */
+ protected function setUp() {
+ parent::setUp();
+ }
+
+ public function testEmptyContactValidate() {
+ $validation = $this->callAPISuccess('Contact', 'validate', array('action' => "create"));
+ $expectedOut = array(
+ 'contact_type' => array(
+ 'message' => "Mandatory key(s) missing from params array: contact_type",
+ 'code' => "mandatory_missing",
+ ),
+ );
+ $this->assertEquals($validation['values'][0], $expectedOut);
+ }
+
+ public function testContributionValidate() {
+ $validation = $this->callAPISuccess('Contribution', 'validate', array('action' => "create", 'total_amount' => "100w"));
+ $totalAmountErrors = array(
+ 'message' => "total_amount is not a valid amount: 100w",
+ 'code' => "incorrect_value",
+ );
+
+ $contactIdErrors = array(
+ 'message' => "Mandatory key(s) missing from params array: contact_id",
+ 'code' => "mandatory_missing",
+ );
+
+ $this->assertEquals($validation['values'][0]['total_amount'], $totalAmountErrors);
+ $this->assertEquals($validation['values'][0]['contact_id'], $contactIdErrors);
+ }
+
+}