I’ve searched the other similar answer but still no luck.
I have content type Station with field Province and City.
Form alter:
function mymodule_form_alter(&$ form, FormStateInterface $ form_state, $ form_id) { if ($ form_id === 'node_station_form') { // kint($ form); $ form['field_province']['widget']['#ajax'] = [ 'event' => 'change', 'callback' => 'mymodule_callback_example', 'wrapper' => 'edit-field-city-wrapper', 'method' => 'replace', ]; $ form['field_city']['#attributes']['id'] = 'edit-field-city-wrapper'; $ options = ['_none' => '- None -']; $ user_input = $ form_state->getUserInput(); $ province = $ user_input["field_province"] ?? NULL; if ($ province) { if ($ province === 'province_a') { $ cities = [ 'province-a-city-1' => 'Province A City 1', 'province-a-city-2' => 'Province A City 2', 'province-a-city-3' => 'Province A City 3', ]; } else { $ cities = [ 'province-b-city-1' => 'Province B City 1', 'province-b-city-2' => 'Province B City 2', 'province-b-city-3' => 'Province B City 3', ]; } $ options += $ cities; } $ form['field_city']['widget']['#options'] = $ options; } } function mymodule_callback_example($ form, FormStateInterface $ form_state) { return $ form['field_city']; }
The dropdown City successfully populate based on Province, but when I click submit form, there are error "The value you selected is not a valid choice"
What did I miss?