Answers.Body: <p>View only fields can only be updated via the API, see: <a href="https://issues.civicrm.org/jira/browse/CRM-17843" rel="nofollow">https://issues.civicrm.org/jira/browse/CRM-17843</a></p>
  
  <p>So you could use one of the hooks (eg: hook_civicrm_pre) to 'detect' when that field has changed, and read the value, and then use the API to set the other value. _pre might be more reliable than the form hooks, as the field might be set by other methods than by submitting a form.</p>
  
  <p>For example:</p>
  
  <pre><code>function foo_civicrm_pre($op, $objectName, $id, &amp;$params) {
    if ($objectName == 'Contact' &amp;&amp; empty($params['custom_xxx'])) {
      // make an API call to set custom_yyy
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-22T11:16:58.997Z
Answers.LastActivityDate: 2016-02-22T11:16:58.997Z
Answers.UserDisplayName: John
Body: <p>I have a Custom Group with two custom fields:
  Field A is a view only input field (ID: custom_32) 
  Field B is a select with a few options (ID: custom_33).</p>
  
  <p>When the user selects one option from Field B and hits save I would like to add a value to Field A based on the selected option.</p>
  
  <p>Which hook shall I use for this?</p>
  
  <p>I tried civicrm_preProcess, civicrm_postProcess and civicrm_validateForm.</p>
  
  <p>My aproach to the problem is to alter the submitted (empty) value in the form object but it doesn't seem to work.</p>
  
  <pre><code>function insert_value_for_b($formName, &amp;$form){
      //determine $newValue
      //other business logic
      $data = &amp;$form-&gt;controller-&gt;container();
      $data['values']['CustomData'][$customIDforB] = $newValue;
  }
  </code></pre>
  
  <p>Could someone advise please?</p>
  
Tags: <wordpress><hooks><forms>
Title: Altering form values
ThreadText: # Altering form values
  
  (Asked by: username_0 on 2016-02-19)
  
  I have a Custom Group with two custom fields:
  Field A is a view only input field (ID: custom\_32)
  Field B is a select with a few options (ID: custom\_33).
  
  When the user selects one option from Field B and hits save I would like to add a value to Field A based on the selected option.
  
  Which hook shall I use for this?
  
  I tried civicrm\_preProcess, civicrm\_postProcess and civicrm\_validateForm.
  
  My aproach to the problem is to alter the submitted (empty) value in the form object but it doesn't seem to work.
  
  ```
  function insert_value_for_b($formName, &$form){
      //determine $newValue
      //other business logic
      $data = &$form->controller->container();
      $data['values']['CustomData'][$customIDforB] = $newValue;
  }
  
  ```
  
  Could someone advise please?
  
  **Tags:** wordpress, hooks, forms
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-02-22. Score: 2 | Accepted answer
  
  View only fields can only be updated via the API, see: <https://issues.civicrm.org/jira/browse/CRM-17843>
  
  So you could use one of the hooks (eg: hook\_civicrm\_pre) to 'detect' when that field has changed, and read the value, and then use the API to set the other value. \_pre might be more reliable than the form hooks, as the field might be set by other methods than by submitting a form.
  
  For example:
  
  ```
  function foo_civicrm_pre($op, $objectName, $id, &$params) {
    if ($objectName == 'Contact' && empty($params['custom_xxx'])) {
      // make an API call to set custom_yyy
    }
  }
  
  ```
  
  
  ---
  
  