在選擇器中只允許兩個指定值的最佳方法是什么,例如我有 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>
uj5u.com熱心網友回復:
使用yup的oneOf驗證器
mix.oneOf(arrayOfValues: Array, message?: string | function): Schema
例如
const signUpSchema = Yup.object().shape({
countrySelector: Yup.string().oneOf(['US', 'UGX']).required('Please select the country'),
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329679.html
標籤:javascript 反应 是的
上一篇:單選按鈕未選擇React.js
