There has been so many requests regarding "live computation" during node add/edit. Here's how i got mine working without breaking anything. This link pointed me to the right direction.
Create a custom module. Change all instances of MYMODULE to the name of your module
/**
* Implementation of hook_form_alter().
*/
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
//here you need to know the form_id of the content type the field is attached to. you can use dpm($form_id) to get this. Let's assume the content type is called 'INVOICE';
case 'invoice_node_form':
//then, you need to target the computed field, in my own case, it is the VAT field. Since computed field module doesn't add #process to the form, it's safe to add yours here. Read more about hook_element_info() <a href="https://www.drupal.org/node/169815">here</a>.
$form['field_vat']['und'][0]['value']['#process'] = array('my_custom_process');
break;
}
}
//now we can make our field appear as a textfield.
function my_custom_process($element, &$form_state, $complete_form){
$element['field_vat'] = array(
'#type' => 'textfield',
'#size' => 8,
'#attributes' => array(
'readonly' => 'readonly',
'tabindex' => -1,
),
);
return $element;
}
Then, your computed field will appear as a text field on the edit page. you can thereafter add a javascript file to autofill the textfield.
Here's a sample below.on keyup or when the value changes on the "rate" field, i want the vat field to be populated with rate * quantity. (Silly calculation)!.
(function ($) {
Drupal.behaviors.mymodule = {
attach: function (context, settings) {
$('.field-name-field-rate').on('keyup change', function (){
var rate = $('.field-name-field-rate input').val();
var quantity = $('.field-name-field-quantity input').val();
var vat= (quantity * rate).toFixed(2);
$('.field-name-field-vat input').val(vat);
});
Note: this is not the actual value saved in the db. It's only for live calculation. Your computed field settings is the one doing the actual calculation.