React Final Form多条件表单验证失效,求解决思路
问题
在React应用中搭建了表单,要求提交后端前完成验证:text、link、image三者填写其一即可。当前验证逻辑失效,仅用单个条件(如!values.text)时验证正常,多条件组合后失效。
表单核心代码:
<div className="card-body"> <h3 className="mt-2 btn-primary text-center"> Diseño ChatBot </h3> <Form onSubmit={onSubmit} validate={values => { const errors = {}; if (!values.text || !values.link ||!values.image) { errors.error = 'Es necesario cumplimentar uno de estos tres campo: Texto, Imagen, Link'; } else {errors.error = undefined;} return errors; }} initialValues={{ title: title, text: text, link: link, link_title: link_title, image: image, image_title: image_title }} render={({ handleSubmit, values, errors }) => ( <form id='editForm' className="row mt-4" onSubmit={handleSubmit} > <div className="col-md-1 mb-4"> <label htmlFor="inputDescription" className="form-label text-center"> <h5> Título </h5> </label> </div> <div className="col-md-11"> <Field type="text" className="form-control" name='title' component="input" /> </div> <div className="col-md-1 mb-4"> <label htmlFor="inputResponse" className="form-label text-center" > <h5> Texto </h5> </label> </div> <div className="col-md-11"> <Field className="form-control" type="textarea" name='text' component="input" /> </div> <div className="col-md-1 mb-4"> <label htmlFor="link" className="form-label text-center" > <h5> Link </h5> </label> </div> <div className="col-md-5"> <Field className="form-control" type="link" name="link" component="input" /> </div> <div className="col-md-1"> <label htmlFor="link_title" className="form-label text-center" > <h5> Title Link </h5> </label> </div> <div className="col-md-5"> <Field className="form-control" type="text" name='link_title' component="input" /> </div> <div className="col-md-1 mb-4"> <label htmlFor="image" className="form-label text-center" > <h5> Image </h5> </label> </div> <div className="col-md-5"> <Field name="image" type="file" component={UploadImage} /> </div> <div className="col-md-1"> <label htmlFor="image_title" className="form-label text-center" > <h5> Title Image </h5> </label> </div> <div className="col-md-5"> <Field className="form-control" type="text" name='image_title' component="input" /> </div> <div> {(isCreated && !isVariationsError) && <div className="alert alert-success" role='alert'> Respuesta EDITADA Correctamente </div> } </div> <div className="buttons text-center"> <Volver className="btn btn-outline-danger me-5 mb-3" prevStep={prevStep} step={2}/> <button className="btn btn-outline-warning me-5 mb-3" onClick={(e) => (e.preventDefault(), openPreview(values))} >Preview</button> <SaveAndEdit className="btn btn-outline-success me-5 mb-3" idForm={'editForm'} text={'Guardar Cambios'}/> <Cancelar className="btn btn-outline-dark me-5 mb-3" /> </div> {errors.error && <Error meta={errors.error} /> } <pre>{JSON.stringify(values, undefined, 2)}</pre> <pre>{JSON.stringify(errors, undefined, 2)}</pre> </form> )} /> </div>
Error组件代码:
const Error = (meta) => ( <h3 className="alert alert-danger text-center mt-5">{meta.meta}</h3> );
解决思路
修正验证逻辑的逻辑运算符
当前代码用!values.text || !values.link || !values.image,逻辑是「只要一个字段为空就报错」,和需求「三个都为空才报错」完全相反。需要把运算符改成&&,同时补充空值的严谨判断:validate={values => { const errors = {}; // 处理空值:空字符串、undefined都视为未填写 const isTextEmpty = !values.text || values.text.trim() === ''; const isLinkEmpty = !values.link || values.link.trim() === ''; // 图片字段判断是否有文件上传 const isImageEmpty = !values.image; // 三个字段都为空时触发错误 if (isTextEmpty && isLinkEmpty && isImageEmpty) { errors.error = 'Es necesario cumplimentar uno de estos tres campo: Texto, Imagen, Link'; } return errors; }}优化字段空值判断细节
- text/link字段:要过滤用户输入的纯空格,用
trim()判断是否为有效内容 - image字段:因为是file类型,直接判断是否存在文件对象即可(需确保UploadImage组件将文件存在
values.image中)
- text/link字段:要过滤用户输入的纯空格,用
简化Error组件参数传递
当前组件接收的是对象,但实际传递的是字符串,可简化组件避免层级冗余:// 修改Error组件 const Error = ({ message }) => ( <h3 className="alert alert-danger text-center mt-5">{message}</h3> ); // 使用时传递对应属性 {errors.error && <Error message={errors.error} /> }
内容的提问来源于stack exchange,提问作者ddaudiosolutions
相关产品推荐
相关产品推荐

