I'm trying to make a module that offers an
#element_validate function which will check the element for dodgy looking data and if this is detected, tell the user this looks invalid and add a 2nd element to the form which can then be used to validate the dodgy-looking data.
The problem I'm having is that:
- To add an element you need the form to rebuild. (see code referenced in the comment below)
- To trigger a rebuild the form must have passed validation
This means that:
#element_validatefunction cannot use
form_error- else it would not be able to add the 2nd field. So I've had it instead set
$form_state['rebuild'] = TRUEand issue a warning with
adding the 2nd element will only happen when there are not any other validation errors on the form (since these would also prevent the form rebuilding) leading to a confusing situation for the user: they will see the warning asking them to complete the 2nd element that doesn't exist. They'll correct the other errors on the form re-submit and then get that back again, this time with the 2nd element, which they'll have to complete before their submission is accepted.
Is there a way around this? e.g. a way to force a rebuild of the form from validation?