簡體   English   中英

驗證只接受兩個特定字段 yup 的選擇器

[英]Validation accept only two specific fields the selector with yup

在選擇器中只允許兩個指定值的最佳方法是什么,例如我有 2 個字段供用戶選擇:

  1. 我們

  2. UGX

下面是我的代碼片段:

驗證代碼:

const signUpSchema = Yup.object().shape({
    countrySelector: Yup.string().required('Please select the country'),
    
});

JSX 代碼段

<Formik
                        validationSchema={signUpSchema}
                        onSubmit={handleSignUpAsync}
                        initialValues={{
                            countrySelector: '',
            
                        }}
                    >
                        <Form>
                            <Field
                                as="select"
                                className="browser-default custom-select"
                                name='countrySelector'>
                                <option value="-">-</option>
                                <option value="DE">DE</option>
                                <option value="US">US</option>
                            </Field>
                        </Form>
                    </Formik>

使用yuponeOf驗證器

mix.oneOf(arrayOfValues: Array, message?: string | function): Schema

例如

const signUpSchema = Yup.object().shape({
    countrySelector: Yup.string().oneOf(['US', 'UGX']).required('Please select the country'),
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM