This is an extension of this question:
Need help with AJAX callback function for text field validation in form_alter
I have a form with a Username autocomplete field (txn_user). The editors want to be able to enter a UID (txn_userid) as an alternative way of specifying the User for the form. They want to be able to enter the UID and skip the Username field altogether.
So in my form validation function, I want to be able to disable the required property (and all other validation) of the Username field during a Submit IF the editor typed in a UID field. In other words: the UID field should ‘short-circuit’ the Username field.
This is what I have so far (which doesn’t work, obviously). Or do I need to do this via AJAX or straight JS?
function _validate_add_userpoints_userid(&$form, &$form_state) { $values = $form_state['values']; if( $values['txn_userid'] ) { $newuser = user_load($values['txn_userid']); if( ! $newuser) { form_set_error('txn_userid', 'You have entered an invalid UID. Either leave this blank and select a Username or enter a valid UID'); } else { // this part works... the User Name is properly assigned. $form['txn_user']['#value'] = $newuser->name; $form_state['values']['txn_user'] = $newuser; $form_state['input']['txn_user'] = $newuser->name; // this part doesn't work... the User Name field is still required on submit. $form['txn_user']['#disabled'] = TRUE; $form['txn_user']['#ajax_processed'] = TRUE; $form['txn_user']['#required'] = FALSE; $form['txn_user']['#needs_validation'] = FALSE; unset($form['txn_user']['#process']); } }
}