我的表格輸入很少。其中之一是檔案輸入,檔案輸入的數量是動態的:
<EuiFilePicker
id={`templateFiles.${index}.documentTemplate`}
name={`templateFiles.${index}.documentTemplate`}
display="default"
accept=".ftl"
onChange={event => {setFieldValue(`templateFiles.${index}.documentTemplate`,event[0])}}
/>
我試圖僅在先前的輸入(contactType)之一是特定值(LETTER)時才需要檔案。我有這樣的事情:
<EuiSelect
options={Object.values(ContactType)}
id={'contactType'}
name={'contactType'}
label={t('pages.participant.communicate.columns.contactType')}
/>
在哪里:
export enum ContactType {
LETTER = 'LETTER',
CALL = 'CALL',
}
和驗證模式:
export const projectContactSchema: SchemaOf<ProjectContactDto> = Yup.object().shape({
{other values...}
contactType: Yup.mixed()
.oneOf(Object.values(ContactType))
.required(i18next.t('pages.participant.communicate.inputs.contactTypeRequired')),
templateFiles: Yup.array()
.of(
Yup.object().shape({
language: Yup.mixed()
.oneOf(Object.values(eLanguage))
.required(i18next.t('pages.participant.communicate.inputs.languageRequired')),
documentTemplate: Yup.mixed().when('contactType', {
is: contactType => contactType === 'LETTER',
then: Yup.mixed().required(i18next.t('pages.participant.communicate.inputs.templateFileRequired')),
otherwise: Yup.mixed().notRequired(),
}),
})
)
.unique('Languages should be unique', (val: any) => val.language)
.notRequired()
.nullable(),
})
但這只是行不通。即使將 contactType 選為 LETTER,也不需要檔案。我正在嘗試其他語法,例如:
is: 'LETTER',
then: ...
但結果是一樣的。
uj5u.com熱心網友回復:
我剛剛找到了一個解決方案,我將'when'陳述句移高了兩步。似乎是的,如果它是嵌套的,則無法檢查條件。
templateFiles: Yup.array()
.when('contactType', {
is: contactType => contactType === 'LETTER',
then: Yup.array().of(
Yup.object().shape({
language: Yup.mixed()
.oneOf(Object.values(eLanguage))
.required(i18next.t('pages.participant.communicate.inputs.languageRequired')),
documentTemplate: Yup.mixed().required('test'),
})
),
otherwise: Yup.array().of(
Yup.object().shape({
language: Yup.mixed()
.oneOf(Object.values(eLanguage))
.required(i18next.t('pages.participant.communicate.inputs.languageRequired')),
documentTemplate: Yup.mixed().notRequired(),
})
),
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/507346.html
上一篇:無法從函式內部回傳變數