0

I have a form with multiple fields. How can I get the value of field B from within the formElement method of my widget class for field A?

1

The formElement method in your widget class should include $form_state as one of its arguments. The value of all the fields in your form should be available in the $form_state object:

example: $form_state->getValue('field_b');

0

If you are writing a widget for a field that's not a good practice to make it dependent to other fields, that does not make sense at all, a widget should be independent, form_state is passed and can be used in form element definition of a field widget but in most cases it is not used to check the data of other fields, in most cases form_state is used in ajax callbacks when you need something like add more button or etc.

So how you should have change your widget dependent on other fields? the answer is you should use form alter solution, in this solution you can change different things about a widget, maybe the widget type at all, like if a select widget is provided with less than 2 options you can hide it as there would be no selection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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