Answers.Body: <p>Monish, I think the problem is that if you use the api to change the total amount &amp; don't pass in other amount values the BAO should assume that net_amount has now changed similarly. I thought we had done something in the BAO - but maybe only on new contributions.</p>
  
  <p>I think at the BAO level adding this logic makes sense</p>
  
  <pre><code>if (contributionTotalAmountHasChanged() &amp;&amp; empty($params['net_amount')) {
     $fee_amount = !isset($params['fee_amount'] ? 
       getFeeAmountFromDB() : $params['fee_amount'];
  
    $params['net_amount'] = $params['total_amount'] - $fee_amount;
  }
  </code></pre>
  
  <p>Possibly some argy bargy around whether $params['fee_amount'] = '' vs $params['fee_amount'] = 0;</p>
  
  <p>At the form level - if someone changes the total_amount then probably we should either 
  1) update the other amounts using js or 
  2) reset them sensibly on submission (e.g like above) &amp; present them with a notice that this has happened.</p>
  
  <p>It's a bit of a pain having to change net_amount &amp; for most users the field is not relevant</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-06T10:40:47.150Z
Answers.Text: Yup that make sense for the cited example by making necessary changes in create fn, but its not just about "Total amount = net amount + fee amount" rule but addresses a major concern for other formrules too. Lets take another rule "Soft Credit Amount <= Total amount", when you create a soft-credit via api against any contribution, of soft-credit amount $100 (corresponding contribution total amount $70) it allows you to create it without any validation error, unlike back-office form.
Answers.UserDisplayName: Monish Deb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-06T08:46:55.217Z
Answers.LastActivityDate: 2017-03-30T13:38:42.680Z
Answers.LastEditDate: 2017-03-30T13:38:42.680Z
Answers.UserDisplayName: eileen
Body: <p>Lemme cite an example before explaining the cause. </p>
  
  <ol>
  <li>Updated the amount of an contribution id n via Contribution.create api from $100 to $150 and "Execute"d it from API Explorer. (No validation error)</li>
  <li>Open the edit back-office contribution form of same contribution ID n. The amount is rightfully changed to $150, but problem is when without doing anything simply save the form and it throws a validation error "Net amount and/or Fee amount must be equal to the Total amount"</li>
  </ol>
  
  <p>The question arises "why didn't I got any validation error at step 1?". Answer lies on the native validation rules written under formRules() func which are not meant for corresponding API.actions. Likewise there are other 
  entity attributes which raise the same concern. </p>
  
  <p>So my suggestion/fix would be to introduce a BAO function for such validation(s), </p>
  
  <blockquote>
    <blockquote>
      <p>Flexible enough to accept input params from both APIs and Forms </p>
      
      <p>Provide API Exception(s) and validation error(s) for APIs and Forms respectively</p>
      
      <p>Optimize API and Form validations</p>
      
      <p>And of course would be a BAO function to get called in both APIs and Forms</p>
    </blockquote>
  </blockquote>
  
  <p>Thoughts ? </p>
  
Tags: <api><validation>
Title: Generalizing create/update validation on form and api
ThreadText: # Generalizing create/update validation on form and api
  
  (Asked by: username_0 on 2015-08-06)
  
  Lemme cite an example before explaining the cause.
  
  1. Updated the amount of an contribution id n via Contribution.create api from $100 to $150 and "Execute"d it from API Explorer. (No validation error)
  2. Open the edit back-office contribution form of same contribution ID n. The amount is rightfully changed to $150, but problem is when without doing anything simply save the form and it throws a validation error "Net amount and/or Fee amount must be equal to the Total amount"
  
  The question arises "why didn't I got any validation error at step 1?". Answer lies on the native validation rules written under formRules() func which are not meant for corresponding API.actions. Likewise there are other
  entity attributes which raise the same concern.
  
  So my suggestion/fix would be to introduce a BAO function for such validation(s),
  
  > > Flexible enough to accept input params from both APIs and Forms
  > >
  > > Provide API Exception(s) and validation error(s) for APIs and Forms respectively
  > >
  > > Optimize API and Form validations
  > >
  > > And of course would be a BAO function to get called in both APIs and Forms
  
  Thoughts ?
  
  **Tags:** api, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-08-06. Score: 1
  
  Monish, I think the problem is that if you use the api to change the total amount & don't pass in other amount values the BAO should assume that net_amount has now changed similarly. I thought we had done something in the BAO - but maybe only on new contributions.
  I think at the BAO level adding this logic makes sense
  if (contributionTotalAmountHasChanged() && empty($params['net_amount')) {
     $fee_amount = !isset($params['fee_amount'] ? 
       getFeeAmountFromDB() : $params['fee_amount'];
  
    $params['net_amount'] = $params['total_amount'] - $fee_amount;
  }
  
  Possibly some argy bargy around whether $params['fee_amount'] = '' vs $params['fee_amount'] = 0;
  At the form level - if someone changes the total_amount then probably we should either 
  1) update the other amounts using js or 
  2) reset them sensibly on submission (e.g like above) & present them with a notice that this has happened.
  It's a bit of a pain having to change net_amount & for most users the field is not relevant
  
  > **Comments:**
  > **username_0**: Yup that make sense for the cited example by making necessary changes in create fn, but its not just about "Total amount = net amount + fee amount" rule but addresses a major concern for other formrules too. Lets take another rule "Soft Credit Amount <= Total amount", when you create a soft-credit via api against any contribution, of soft-credit amount $100 (corresponding contribution total amount $70) it allows you to create it without any validation error, unlike back-office form.  
  
  ---
  
  