draftjs - 通过 Formik 中的 yup 验证 Draft-js EditorState
问题描述
我正在使用带有 Formik 的 Draft-js 和yup来验证 Draft EditorState 组件。
我假设我可以通过使用以下 yup 测试来检查 EditorState 是否为空白:
//Each note has a string title and a body, which is a Draft EditorState
const validationSchema = yup.object().shape({
title: yup.string(),
body: yup.object()
.test("has text", "Cannot save an empty note", (value) => {
return value.getCurrentContent().hasText(); //boolean
}),
})
但是,我收到错误:
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined at yupToFormErrors (formik.esm.js:491) at formik.esm.js:174
这是formik的错误,还是我使用错误?
其他信息:
在validationSchema 中,我得到以下信息:
console.log(value.getCurrentContent().hasText()) //returns undefined
console.log(value.getCurrentContent()) //returns undefined
但在 EditorState ('body' 字段)的 onChange 处理程序中,它可以正常工作:
console.log(value.getCurrentContent().hasText()) //returns true or false
解决方案
这适用于我的版本 0.10.5:
value._editorState.getCurrentContent().hasText()
推荐阅读
- spring-boot - 高 CPU 使用率和 ActiveMQ Artemis 中许多队列和会话的负载
- vim - 在 Vim 中的每一行末尾添加一个反斜杠
- laravel - Git-win10-laragon-权限被拒绝
- asp.net-mvc - 如果在响应标头中同时传递 X-Frame-options SAMEORIGIN 和 DENY 会发生什么?
- excel - Excel VBA - 改进全局变量使用的技巧
- amazon-ec2 - 如何使用 ec2_sd_config 和 blackbox exporter 进行多目标和多端口服务状态检查。?
- python - ValueError:使用序列设置数组元素。ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型系列)
- java - 我的代码实现 RSA 有什么问题?
- c# - NSubstitute 在基准测试中分配内存
- shapefile - 地理绘图英国(ONS)县形状文件(shp)的问题