I want to do some calculations in node edit form vith ajax. Form fields is created via UI, so i need to drill into field array to get value(s). Maximum result what i have is replacing target field with result of #defaul_value of source fields. Nothing happens when i change source.

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;

function module_form_alter(&$form, &$form_state, $form_id) {
    $form['field_source']['widget'][0]['value']['#ajax']  = array(
        'callback' => 'myAjaxCallback',
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'change',
        'wrapper' => 'calc',

    $form['field_source2']['widget'][0]['value']['#ajax']  = array(
        'callback' => 'myAjaxCallback',
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'change',
        'wrapper' => 'calc',

    $form['field_target']['#prefix'] = '<div id="calc">';
    $form['field_target']['#suffix'] = '</div>';
    $form['field_target']['#disabled'] = TRUE;
 /* some calculations */
    $s1 = $form['field_souce']['widget'][0]['value']['#default_value']; /*  HOW TO GET PROPER SOURCE VALUES "ON FLY" WHEN EDITOR CHANGES IT? */
    $s2 = $form['field_souce2']['widget'][0]['value']['#default_value'];
    $result = $s1 * $s2;

    $form['field_target']['widget'][0]['value']['#default_value'] = $result;

function myAjaxCallback($form, FormStateInterface $form_state)  {
   return $form['field_target'];

Calculations gives result, but only with values which already submitted, so when i change source values AJAX firing & fading, but nothing happens whith target. What i try is get values with:

$s1 = $form_state->getValue('source'); <-- gives "0" $result
$s1 = $form['field_souce']['widget'][0]['value'] <-- get error Unsupported operand types

2 Answers 2


So, you can provide not raw and validated way – Otto Matik

The proper & safe way is to get them using $form_state->getValue('field_name')

$field_source_values = $form_state->getValue('field_source');
$s1 = $field_source_values[0]['value'];

$field_source2_values = $form_state->getValue('field_source2');
$s2 = $field_source2_values[0]['value'];

OR if you like you could use $form_state->getValues() and you'll get an array of all the fields with their values.


found a way to get input value

$values = $form_state->getUserInput();
$s1 = $values['field_source']['0']['value'];
$s2 = $values['field_source2']['0']['value'];
  • 1
    Should be noted that getUserInput says, These are raw and unvalidated, so should not be used without a thorough understanding of security implications.
    – No Sssweat
    Commented Jun 8, 2021 at 4:52
  • So, you can provide not raw and validated way
    – Otto Matik
    Commented Apr 30 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.