- Issue created by @jeffschuler
@jeffschuler You can code
hook_element_info_alter
and pass element validation there./** * Implements hook_element_info_alter(). */ function module_name_element_info_alter(array &$info) { if (isset($info['tablefield'])) { $info['tablefield']['#element_validate'][] = 'module_name_tablefield_validate'; } } /** * Form element validation handler for #type 'tablefield'. */ function module_name_tablefield_validate(&$element, FormStateInterface $form_state, &$complete_form) { // TODO }
- Status changed to Fixed
12 months ago 7:50pm 17 October 2023 - πΊπΈUnited States jeffschuler Boulder, Colorado
Thanks @ad0z! We'll give this a try and I'll re-open if we have issues.
- Status changed to Active
11 months ago 1:36pm 25 October 2023 - πΊπΈUnited States jeffschuler Boulder, Colorado
Couldn't get
hook_element_info_alter()
to work, but was able to set a validator inhook_field_widget_WIDGET_TYPE_form_alter()
(our tablefield is inside a paragraph) such that when the form is submitted, the number of columns are checked and a form error is thrown if above our max.However, I want to validate and set the error on the submission of the AJAX rebuild functionality itself ~ when the Rebuild button is clicked. (I don't want the user to be able to rebuild their table above max rows and then not figure it out until the submit the whole node.)
Seems like this is because the
$element['tablefield']['rebuild']['rebuild']
sub-form is built in theprocessTableField()
function and doesn't yet exist when form alters happen.I tried setting an
#after_rebuild
hook on the form, but$element['tablefield']['rebuild']
still wasn't set, though$element['tablefield']['#rebuild']
was true... >Couldn't get hook_element_info_alter() to work
I was checking it before I recommended it and it's working or there is misunderstood between us.
Here is fast example which is working for me:/** * Implements hook_element_info_alter(). */ function custom_module_element_info_alter(array &$info) { if (isset($info['tablefield'])) { $info['tablefield']['#element_validate'][] = 'custom_module_tablefield_validate'; } } /** * Form element validation handler for #type 'tablefield'. */ function custom_module_tablefield_validate(&$element, FormStateInterface $form_state, &$complete_form) { if ($form_state->getTriggeringElement()['#name'] === 'tablefield-rebuild-field_test_table-0') { $values = $form_state->getValues()['field_test_table'][0]['tablefield']['rebuild']; $cols = $values['cols']; $rows = $values['rows']; if ($rows > 10) { $form_state->setError($element['tablefield']['rebuild']['rows'], 'Rows error'); } if ($cols > 10) { $form_state->setError($element['tablefield']['rebuild']['cols'], 'Cols error'); } } }
- πΊπΈUnited States jeffschuler Boulder, Colorado
@ad0z: thank you.
After some debugging, I see that the reason the validation callback wasn't being called is because of this change that I made in patch #9 on π If required, all cells need not be full Needs review :
- if ($form_state->getTriggeringElement()) {
- $element['#element_validate'][] = [$this, 'validateTablefield'];
- }
+ $element['#element_validate'][] = [$this, 'validateTablefield'];Some more digging to do...