Repository: strapi/strapi
File: packages/core/content-manager/admin/src/pages/EditView/components/DocumentActions.tsx (TypeScript)

Code before:
      model: parentDocumentMetaToUpdate?.model,
      collectionType: parentDocumentMetaToUpdate?.collectionType,
      params: parentDocumentMetaToUpdate?.params,
    },
    { skip: !parentDocumentMetaToUpdate }
  );
  const { getInitialFormValues } = useDoc();

  const isDocumentPublished =
    (document?.[PUBLISHED_AT_ATTRIBUTE_NAME] ||
      meta?.availableStatus.some((doc) => doc[PUBLISHED_AT_ATTRIBUTE_NAME] !== null)) &&
    document?.status !== 'modified';

  if (!schema?.options?.draftAndPublish) {
    return null;
  }

  const performPublish = async () => {
    setSubmitting(true);

    try {
      const { errors } = await validate(true, {
        status: 'published',
      });
      if (errors) {
        let hasUnreadableRequiredField = false;
        for (const fieldName of Object.keys(schema.attributes)) {
          const attribute = schema.attributes[fieldName];

          if (attribute && attribute.required) {
            const userCanReadThisField = (canReadFields ?? []).includes(fieldName);

            if (!userCanReadThisField) {
              hasUnreadableRequiredField = true;
              break;
            }
          }
        }

        if (hasUnreadableRequiredField) {
          toggleNotification({
            type: 'danger',
            message: 'Test error about unreadable required field', // TODO: the new error message ??
          });
        } else {
          toggleNotification({
            type: 'danger',
            message: formatMessage({
              id: 'content-manager.validation.error',
              defaultMessage:
                'There are validation errors in your document. Please fix them before saving.',

Review comment (refactor):
Without a more significant refactor of this component, **I think the only scenario we can cover with a new specific error message here is when the user cannot read a required field.**

All the publishAction logic is based off "formValues". That is whats seen in the frontend form and what is sent to the publish action in the end.

Even if the draft in the DB has a value in the required field, if the user cannot read that field then it's value in form values will be null/undefined.

So we should always error with a message that says something like "There is a required field without a value". Even if in the DB this field does hold a value (this component doesn't know about that DB value). From the admin users perspective it doesn't have a value (they can't read the field).

Code after:
      model: parentDocumentMetaToUpdate?.model,
      collectionType: parentDocumentMetaToUpdate?.collectionType,
      params: parentDocumentMetaToUpdate?.params,
    },
    { skip: !parentDocumentMetaToUpdate }
  );
  const { getInitialFormValues } = useDoc();

  const isDocumentPublished =
    (document?.[PUBLISHED_AT_ATTRIBUTE_NAME] ||
      meta?.availableStatus.some((doc) => doc[PUBLISHED_AT_ATTRIBUTE_NAME] !== null)) &&
    document?.status !== 'modified';

  if (!schema?.options?.draftAndPublish) {
    return null;
  }

  const performPublish = async () => {
    setSubmitting(true);

    try {
      const { errors } = await validate(true, {
        status: 'published',
      });
      if (errors) {
        const hasUnreadableRequiredField = Object.keys(schema.attributes).some((fieldName) => {
          const attribute = schema.attributes[fieldName];

          return attribute?.required && !(canReadFields ?? []).includes(fieldName);
        });

        if (hasUnreadableRequiredField) {
          toggleNotification({
            type: 'danger',
            message: formatMessage({
              id: 'content-manager.validation.error.unreadable-required-field',
              defaultMessage:
                'Your current permissions prevent access to certain required fields. Please request access from an administrator to proceed.',
            }),
          });
        } else {
          toggleNotification({
            type: 'danger',
            message: formatMessage({
              id: 'content-manager.validation.error',
              defaultMessage:
                'There are validation errors in your document. Please fix them before saving.',
            }),
          });
        }
        return;