I have a workflow with 3 different states (draft, review, publish). Now I want to limit validations for few fields when submitting with draft state. For that I have added $form['actions']['workflow_course_status_save_as_draft']['#limit_validation_errors'] = array(); still all the fields are getting validated.

I have checked without workflow and it is working fine. So from the looks of it, I think Workflow module is overriding these settings.

Comments

sriharsha.uppuluri created an issue. See original summary.

johnv’s picture

Status: Active » Postponed (maintainer needs more info)

I did a quickscan of the Workflow code. It is possible , if there is an error in function workflow_form_alter().

But only if you have set the 'state list' to 'action buttons', not to radio_buttons.
How is your configuration on /admin/config/workflow/workflow/MY_WORKFLOW/edit under 'How to show the available states' ?

If possible debug the code of function workflow_form_alter() in file workflow.form.inc

nicobot’s picture

I have the same situation with Workflow 7.x-2.10; when I set #limit_validation_errors in a workflow button, it stops saving any transition.

Any idea why is this happening?

nicobot’s picture

So I figured this out. The reason why this is failing is that we're not using properly #limit_validation_errors. This issue has nothing to do with Workflow.

During the form validation, if this property is set (validation errors are limited), Drupal core will remove any non validated form values from the form state, so only values that passed validation will be left for submit callbacks.

For Drupal 8:
http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Core/Form/FormVal...

For Drupal 7:
http://cgit.drupalcode.org/drupal/tree/includes/form.inc?h=7.x#n1193

My solution was to add a button validation that will be executed at end of all validations, and in there remove all form-related errors:


/**
 * Removes all validation errors that could happen when saving a draft
 *
 * @param $form
 * @param $form_state
 */
function my_custom_draft_validation($form, &$form_state) {
  $errors = form_get_errors();

  if (!empty($errors)) {
    $messages = drupal_get_messages('error', TRUE);

    // Remove error messages originated during the form validation
    foreach ($messages['error'] as $error) {
      if (!in_array($error, $errors)) {
        drupal_set_message('error', $error);
      }
    }

    // Remove all form errors
    form_clear_error();
  }
}
johnv’s picture

OK, thanks for the heads-up. So what is the status now?
Should something be changed in the Workflow module, or is all OK?
I didn't know about '#limit_validation_errors', but maybe it should be implmented?

johnv’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)