2

I need to make an entity reference field in my form be required based on the state, the code below is working with other fields, but not my entity reference field.

$form['schedule_publish_date'] ['#states'] =  array(
 'required' => array(
  ':input[name="moderation_state[0]"]' => array('value' =>"published"),
 ),
);

this is the source code of the entity reference field. Am I putting in the wrong name for the form item?

<fieldset data-drupal-selector="edit-schedule-publish-date" id="edit-schedule-publish-date" class="js-form-item form-item js-form-wrapper form-wrapper">
  <legend>
<span class="fieldset-legend">Schedule Publish Date</span>

      </div>

the moderation state is not what I am having trouble reaching. It's the schedule publish date, which is an entity reference and doesnt become "required" when it should.

  • Could you please mention what modules you are using ? For example, where comes from the "moderation_state" input ? Are you sure with the value you are setting ? Have a look to this question would be interesting for you I think. – guilhom Jul 10 '17 at 15:40
  • Be aware that using states to make a value required is not a guarantee. It uses client side JavaScript that could be disabled or altered. You should always include a form validation callback to make sure the value is really there after submission. – Neograph734 Jul 12 '17 at 9:30
1

There isn't a specific form element with the moderation state, since they are added as action submit buttons, but you could add one like this:

  if (!empty($form['moderation_state']['widget'])) {
    $form['moderation_state_hidden'] = [
      '#type' => 'hidden',
      '#title' => 'moderation_state_hidden',
      '#value' => $form['moderation']['widget'][0]['#default_value'],
    ];
  }

Then you could refer to it in your states array like this:

$form['schedule_publish_date'] ['#states'] =  array(
 'required' => array(
  ':input[name="moderation_state_hidden"]' => array('value' =>"published"),
 ),
);

But using states doesn't really make sense because you can't change the moderation status of a hidden field or a non-ajax submit button.

So in that case, you could just update the form element to be required

  if (!empty($form['moderation_state']['widget']) && $form['moderation_state']['widget'][0]['#default_value'] == 'published') {
    $form['schedule_publish_date']['widget'][0]['target_id']['#required'] = TRUE;
  }
| improve this answer | |
  • But using states doesn't really make sense because you can't change the moderation status of a hidden field or a non-ajax submit button. Very good point. just wrap the whole $form['schedule_publish_date'] in an if-statement depending on the published state instead. – Neograph734 Jul 10 '17 at 17:45
  • no the moderation state is not what i am having trouble reaching. its the schedule publish date, which is an entity reference and doesnt become "required" when it should – Diana Jul 10 '17 at 18:23
  • I have updated code for how to access target_id of a entity reference field, see the part like this: ['widget'][0]['target_id']['#required'] = TRUE; – oknate Jul 10 '17 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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