[英]How can i make controller component for Form Element
In react native I want to make a dynamic controller component.在本机反应中,我想制作一个动态 controller 组件。 But i cant access errors with it.
但我不能用它访问错误。 I using "react-hook-form" for form elements.
我对表单元素使用“react-hook-form”。 So Its my component:
所以它是我的组件:
const {
control,
handleSubmit,
formState: {errors},
setValue,
} = useForm();
const DynamicController = ({req, pattern, name, label}) => {
return (
<>
<Text style={[t.textBase]}>{label}</Text>
<Controller
control={control}
defaultValue=""
rules={{
required: {
value: true,
message: 'Bu alan boş bırakılamaz!',
},
}}
render={({field: {onChange, onBlur, value}}) => (
<Input
errorText={errors[name].message}
error={errors[name]}
onBlur={onBlur}
placeholder={label}
onChangeText={onChange}
value={value}
/>
)}
name={name}
/>
</>
);
};
My Input Component is basicly simple input.我的输入组件基本上是简单的输入。 My problem is when i give error name like that example i cant access errors.
我的问题是当我像那个例子一样给出错误名称时,我无法访问错误。
Its how i use my component:我如何使用我的组件:
<DynamicController
label="Email"
name="Email"
pattern={true}
req={true}
/>
When i dont fill the element and log the submit its not showing any error.当我不填写元素并记录提交时,它没有显示任何错误。 Its simple passing validate.
其简单的通过验证。 So what can i do where do i make wrong?
那我该怎么办我哪里做错了? thank you for answerings!!!
谢谢你的回答!!!
Is your Input a custom wrapper?您的 Input 是自定义包装器吗? If not, a better way do this using react-hook-form would be:
如果没有,使用 react-hook-form 的更好方法是:
const {
control,
handleSubmit,
formState: {errors},
setValue,
} = useForm(
defaultValues: {
firstName: '', // form fields should be populated here so that the error can be displayed appropriately
lastName: ''
}
);
const DynamicController = ({req, pattern, name, label}) => {
return (
<>
<Text style={[t.textBase]}>{label}</Text>
<Controller
control={control}
defaultValue=""
rules={{
required: {
value: true,
message: 'Bu alan boş bırakılamaz!',
},
}}
render={({field: {onChange, onBlur, value}}) => (
<Input
onBlur={onBlur}
placeholder={label}
onChangeText={onChange}
value={value}
/>
)}
name={name}
/>
{errors[name] && <Text>This is required.</Text>}
</>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.