Score:1

Using a #submit callback on a textfield

cn flag

We have a need to add 'repeatable fields' to a form based on input provided in a textfield. Usually, we use a 'add another' button to keep track of the number of fields to render in the buildForm(), and update that number in a submit callback on the button.

The functionality we want isn't that, though. We have a need so that when somebody enters text into a textfield (other than empty), it adds an additional group of fields below it. We are allowing people to scan barcodes, and every time they scan a barcode into a textfield, we want it to create another group of fields so they can scan another item if they want to.

We attempted to use #submit on the textfield, but #submit doesn't appear to trigger on the textfield, as we added a print statement, as well as a $form-state->set() in there, and attempt to print out $form_state->get() on the form to no avail.

Since you can't update the form_state inside of an ajax callback, we can't figure out a way to achieve what we want to do (Add another group of fields once text is entered into a textarea).

Is there another way we can do this?

(We only allow 10 groups to be added per submission, so as a 'workaround' we dynamically add 10 groups in the buildForm, then use #states to hide each one if the previous textfield doesn't have data, but this doesn't allow us to add in a 'remove' button for each textfield so the user can remove text fields they don't want to use.)

4uk4 avatar
cn flag
The basic workflow of an Ajax request is to check in the form rebuild `$form_state->getTriggeringElement()` and build a form with as many new form element as you like.
Gold avatar
kz flag
The 'repeatable fields' angle on this feels like what [Inline Entity Forms](https://www.drupal.org/project/inline_entity_form) would address. These work fine with the States API and also allow you to set a min/max/unlimited number of items.
Ex0r avatar
cn flag
This is a completely module based form all generated and functional inside of a buuldForm() method. There are no entities attached to the form.
Ex0r avatar
cn flag
And as far as $form_state, Im already getting the trigger element, but I cant update the form state inside of an ajax callback so I can't rebuuld the form with the needed number of fields.
Jaypan avatar
de flag
You can add an ajax enabled submit button with CSS, then use JS to 'click' it on keyup (or on blur or whatever) triggering the #ajax. You can put your submit handler on the submit button.
4uk4 avatar
cn flag
There is no need for a submit handler when rebuilding a form via Ajax. Whatever you are trying to do in the submit handler move it to buildForm() checking the triggering element in $form_state.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.