1

While adding a new submit button to the form

$form['actions']['save_as_draft'] = [
  '#type' => 'submit',
  '#value' => t('Save as Draft'),
  '#submit' => ['::submitForm', '::save', 'save_as_draft'],
  '#validate' => [],
  '#limit_validation_errors' => []
];
$form['#validate'] = [['validate_errors']];

This does not save the entity. I am not sure how to edit the following validate function.

function validate_errors(&$form,FormStateInterface $form_state) {
  if ($form_state->getTriggeringElement()['#value'] == t('Save as Draft')) {
    // Skip validation.
  }else {
    // Validate here.
  }
}

Meanwhile, I got the error when saving (Entity validation was skipped): enter image description here

Any inputs on how I can fix this and allow the entity to be saved without validation?

1
  • Have you ever figured out how to save the entity? I tried using $entity = $form_state->getFormObject()->getEntity() and then $entity->save(); in my custom submit handler but in vain.
    – jozi
    Commented Jun 20, 2022 at 5:02

1 Answer 1

1

I had the same problem. Instead of "#limit_validation_error", adding validation handler which simply omits all the error works fine.

function my_module_form_alter(&$form, $form_state, $form_id) {
  $form['actions']['save_as_draft'] = [
    '#type' => 'submit',
    '#value' => t('Save as Draft'),
    '#submit' => ['::submitForm', '::save',],
    '#validate' => ['_my_module_clear_errors'],
  ];

  // Add this to avoid 'Entity validation was skipped' error.
  $form_state->setTemporaryValue('entity_validated', TRUE);
}

// Remove all errors.
function _my_module_clear_errors(&$form,FormStateInterface $form_state) {
  $form_state->clearErrors();
}

This comment was really helpful

https://www.drupal.org/project/workflow/issues/2859053#comment-12140370

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.