How can I skip field validation if the field is hidden? I get a node form with node_form and then I render it with some modifications.

One of the modifications is that I duplicate the fields so I have the same fields twice. One field is disabled and the other same field is enabled, I also have a checkbox to show or hide the disabled/enabled field with #states. So I have only one field visible at time, for example, I have a disabled field visible, if I check the checkbox then disabled field is hidden and enabled field is shown.

The problem comes with the #requried = TRUE fields. When I submit the form it validates the two fields (enabled and disabled) but I only want to validate the visibile field. So, if it's visible the disabled field, I don't want to validate the enabled field.

What is a good way to achieve this?
I've tacked a variation on this before and this was the only solution I found that didn't have problems of some sort.

The key is not to skip all validation as you may add security holes so you have to specifically skip required validation. - Specifically what I'm getting at here is don't use #limit_validation_errors.

You cannot stop drupal core from validating that required fields are not empty, so you have to make the fields not required when they get validated.

Note that this example will make all fields not required, so you'll have to add some logic to only do that for the fields you are interested in.
I couldn't add this because I don't have enough info about your setup.

In your form add this:

  // Add an after build callback so we can modify the form before validation.
  $form['#after_build'][] = 'MYMODULE_form_after_build';


Then add that after build function and a helper function:

/**
 * Form after build callback.
 *
 * We are using this to modify the form after it has been submitted,
 * but before it has been validated.
 * This means we can remove required flags for certain fields.
 *
 * @param array $element
 *   An associative array containing the structure of a form element.
 *   In this case the form.
 * @param array $form_state
 *   The form state array.
 *
 * @return array
 *   An associative array containing the structure of a form element.
 */
function MYMODULE_form_after_build($element, &amp;$form_state) {
  // Check that the form has been submitted.
  if ($form_state['process_input']) {
    // Deal with required fields.
    _MYMODULE_remove_required($element);
  }
  return $element;
}

/**
 * Make elements of a form not required.
 *
 * This example function will make all required fields not
 * required.
 * In your case you want to only do certain fields so you will have
 * to add logic in here to only affect some fields.
 * You could do this based on field name or some other flag you have
 * set, or whatever.
 *
 * @param array $elements
 *   An associative array containing the structure of a form.
 */
function _MYMODULE_remove_required(&amp;$elements) {
  // Recurse through all children.
  foreach (element_children($elements) as $key) {
    if (isset($elements[$key]) &amp;&amp; $elements[$key]) {
      _MYMODULE_remove_required($elements[$key]);
    }
  }
  if (!empty($elements['#required'])) {
    $elements['#required'] = FALSE;
  }
}