form doesn't get submitted if i use dynamic show #5206
-
@bluebill1049 Hi Bill, I have a checkbox in my component. If you have it turned off, it means my form shows input type My checkbox is called This is my rest of the code. {!watch('isRuleFile') ? (
<Controller
name="daoConfig.rules"
control={control}
defaultValue={''}
rules={{ required: 'This is required.' }}
render={({
field: { onChange, value },
fieldState: { error },
}) => (
<InputField
label=""
onInputChange={onChange}
value={value.toString()}
height={'100px'}
width={'100%'}
placeholder={'DAO rules and agreement ...'}
error={!!error}
helperText={error ? error.message : null}
/>
)}
/>
) : (
<div
style={{
width: 'inherited',
display: 'flex',
flexDirection: 'row',
verticalAlign: 'middle',
lineHeight: '40px',
marginTop: '17px',
}}
>
{
//@ts-ignore
<input
{...register('rulesFile', { required: true })}
type="file"
/>
}
{errors.rulesFile &&
'Please upload a file, otherwise switch to Text format.'}
<ANButton
label={'Upload to IPFS'}
buttonType={'primary'}
onClick={uploadRulesToIpfs}
style={{ marginTop: '34px' }}
disabled={!watch('rulesFile') || watch('rulesFile').length == 0}
/>
</div>
)} And in the end of my form, I got this: <ANButton
// disabled={!isConnected}
label={'Save settings'}
buttonType={'primary'}
onClick={handleSubmit(callSaveSetting)}
style={{ marginTop: '34px' }}
width={'100%'}
/> Now, The problem is that if I turn on the checkbox, which causes that my form loses I'd love to know what's going on and if I am doing something really wrong. Thanks for such a great plugin. Really appreciate it . |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
can you provide a simple codesandbox for the above? |
Beta Was this translation helpful? Give feedback.
-
I believe when the input is hidden, You can use I suggest using <Controller
rules={{
validate: (value) => {
// field is valid by default if `isRuleFile` is `false`
if (!isRuleFile) return true
const isValid = /* your logic here */
if (!isValid) {
return 'Your error message'
}
return true /* value is valid */
}
}}
/> |
Beta Was this translation helpful? Give feedback.
I believe when the input is hidden,
react-hook-form
still validates the input, which triggers therequired
rules.You can use
handleSubmit(onSuccess, onError)
to check if that is the case.I suggest using
validate
in this case: