I have a problem with validating a Drupal 6 form.
I have 2 dropdowns. The options of the second dropdown (Project) depend on the selected option of the first dropdown (Client). When changing the client, I get all projects from this client.
Now, when the form is validated, the and an other client as the default_value is selected, the options for the projects are not the same as in the form building. That means, the selected option of the project is not in the options array which was built for the form.
Is there any way that the options array can be altered for the form validation? That the options array for the project in the validation is built depending on the selection of the client?
The main thing to realize here is that your form build function is not only called once for initial form building, but again on form submission, before validation takes place. So you can adjust your form build function to build a different $options array for the project select field, depending on the selected client (if any selected).
You'd do this by checking $form_state['values'], somewhat like so:
function your_form(&$form_state) {
// ... other form building stuff
// Start with initial default options for project select
$project_options = array('- Please select client first -');
// Adjust, if client already selected
if (isset($form_state['values']) && $form_state['values']['your_client_select']) {
$selected_client = $form_state['values']['your_client_select'];
$project_options = your_function_to_build_project_options_by_client($selected_client);
}
// ... build project select using those options
// ... other form building stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With