Custom Validation after Custom submit How to pass the custom validation after custom submission?

I have problem in getting a variable from a custom submit handler. Actually, the custom validation must happen in the last step in order to validate correctly.

Workflow is:

1.custom submit.

2.get a value from the API.

3.pass it to custom validation.

Since the custom validation step comes first, I am unable to validate it properly.
You can use form_set_value() in your validation handler. Let's say you are retrieving a user ID from a remote API, by sending the user's name. In your form definition, you can have:

$form['user'] = array (
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('User to fetch'),
);


In your validation handler, you can retrieve the value. If a value exists you save it using form_set_value, and if it doesn't, you set an error:

$remote_user_id = retrieve_uid_from_api($form_state['values']['user']);
if($remote_user_id) {
  form_set_value($form['user'], $remote_user_id);
}
else {
  form_set_error('user', t('%user does not exist in the remote API', array('%user' =&gt; $form_state['values']['user']));
}


So in your submit function, `$form_state['values']['user'] will now contain the User ID from the remote API:

$remote_user_id = $form_state['values']['user'];