Есть вот такая форма ,не получается вызвать callback .По идее при выборе селекта выбранное значение должно записываться в текстовое поле ,но сейчас только зависает .
При разных способах вызова - разные ошибки
'callback' => '::myAjaxCallback', ---- The specified #ajax callback is empty or not callable.
'callback' => [get_called_class(), 'myAjaxCallback'], --- TypeError: Argument 1 passed to Drupal\\Core\\Render\\MainContent\\AjaxRenderer::renderResponse() must be of the type array, null given, called in
Может кто-то сталкивался ,подскажите пожалуйста.
<?php
public static function processAddress(array &$element, FormStateInterface $form_state, array &$complete_form) {
$element['example_select'] = [
'#type' => 'select',
'#title' => 'Example select field',
'#options' => [
'1' => 'One',
'2' => 'Two',
'3' => 'Three',
'4' => 'From New York to Ger-ma-ny!',
],
'#ajax' => [
// 'callback' => '::myAjaxCallback', // don't forget :: when calling a class method.
'callback' => [get_called_class(), 'myAjaxCallback'], //alternative notation
'disable-refocus' => FALSE, // Or TRUE to prevent re-focusing on the triggering element.
'event' => 'change',
'wrapper' => 'edit-output', // This element is updated with this AJAX callback.
],
];
$element['output'] = [
'#type' => 'textfield',
'#size' => '60',
'#disabled' => TRUE,
'#value' => 'Hello, Drupal!!1',
'#wrapper' => 'edit-output',
'#prefix' => '<div id="edit-output">',
'#suffix' => '</div>',
];
if (!empty(
$value['country_code'])) {
$element = static::addressElements($element, $value);
}
return
$element;
}
public function
myAjaxCallback(array &$element, FormStateInterface $form_state) {
// Prepare our textfield. check if the example select field has a selected option.
if ($selectedValue = $form_state->getValue('delivery_methods_select')) {
// Get the text of the selected option.
$selectedText = $element['example_select']['#options'][$selectedValue];
// Place the text of the selected option in our textfield.
$element['output']['#value'] = $selectedText;
}
// Return the prepared textfield.
return $element['output'];
}
?>
Комментарии
статики вперемешку с нестатиками
не знаю, кто вам это посоветовал, но не надо так делать