0

I want to disable the submit button in a form for as long as there are no value/uploaded video in a managed_file but I can't seem to find what form name or element property I have to check for in #states.

I have the following:

$form['file'] = [
  '#type' => 'managed_file',
  '#title' => t('File'),
  '#description' => $this->t('Choose video to upload.'),
  '#upload_validators' => $validators,
];
$form["submit"] = [
  "#type" => "submit",
  "#value" => t('Submit'),
  '#states' => [
    'enabled' => [
      ':input[name="file"]' => ['empty' => FALSE],
    ],
  ],
];

I've also tried using value => true, :input[name="files[0] and :input[name="files[file] without luck. How can I enable the submit button when file has been processed?

1 Answer 1

0

If you examine the comments in the documentation, there are several things to look out for:

  1. You may need to target the element's widget, as mentioned in this comment. Most likely you would do this in a #process callback or in a form alter hook.

This is a good nuance to understand - thanks! Targeting the widget worked for me on a number field. It gets ignored otherwise.

// My number field is hidden unless repeat is selected
$form['field_myfield_num']['#states'] = [
  'visible' => [
    ':input[name="field_myfield_select"]' => ['value' => (string) 'repeat'],
    ],
];

// My number field is set as required when repeat is selected.
$form['field_myfield_num']['widget']['0']['value']['#states'] = [
  'required' => [
    ':input[name="field_myfield_select"]' => ['value' =>  (string) 'repeat'],
  ],
];
  1. In the same comment above, we also see that sometimes it helps to cast your candidate value to a string: ['value' => (string) 'repeat']. I've also seen this explicitly mentioned elsewhere, but can't find the reference.

  2. It is possible to test for a negation using '!value', as seen in this comment:

I had a form with a field of #type select_or_other, which renders like radios. I wanted to enable the submit button when the form element was not "none". The following worked for me:

$form['actions']['submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Donate'),
  '#button_type' => 'primary',
  '#states' => [
    'enabled' => [
      ':input[name="amount[select]"]' => ['!value' => 'none'],
    ],
  ],

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.