admin管理员组文章数量:1287526
Whats the best way to allow only two specified values in the selector, for example I have 2 fields for a user to select :
US
UGX
below is my code snippet :
Validations code :
const signUpSchema = Yup.object().shape({
countrySelector: Yup.string().required('Please select the country'),
});
JSX snippet
<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>
Whats the best way to allow only two specified values in the selector, for example I have 2 fields for a user to select :
US
UGX
below is my code snippet :
Validations code :
const signUpSchema = Yup.object().shape({
countrySelector: Yup.string().required('Please select the country'),
});
JSX snippet
<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>
Share
Improve this question
edited Oct 21, 2021 at 12:20
Lutaaya Huzaifah Idris
asked Oct 21, 2021 at 12:08
Lutaaya Huzaifah IdrisLutaaya Huzaifah Idris
4,0109 gold badges43 silver badges86 bronze badges
1 Answer
Reset to default 13Use the oneOf
validator from yup
mixed.oneOf(arrayOfValues: Array, message?: string | function): Schema
e.g.
const signUpSchema = Yup.object().shape({
countrySelector: Yup.string().oneOf(['US', 'UGX']).required('Please select the country'),
});
本文标签: javascriptValidation accept only two specific fields the selector with yupStack Overflow
版权声明:本文标题:javascript - Validation accept only two specific fields the selector with yup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741226008a2361870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论