反应:输入验证 [英] React: input validation

查看:69
本文介绍了反应:输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用React,并且在输入验证方面遇到了问题.例如,它只是通过指令在另一个框架中以Angular.js的形式实现.

I recently started working with React and I faced the problem with inputs validation. For example, it simply implemented in another framework as Angular.js via directives.

经过研究,我发现

  1. newforms 库,看起来像是当前时刻的方框.但是它很重,并且不确定当前是否支持它(最近更新是7个月前).
  2. 另一种方法是将事件从父窗体发送到其子输入,并在每个子窗体上调用验证方法.

但是我找不到每个人都尝试发明自己的东西的最佳实践,因此您需要编写自己的东西.

But I could not find the best practices everyone tries to invent something own and as a result you need to write something own.

什么是表单验证的最佳解决方案?React架构/框架(Flux/Redux)是否提供任何解决方案?

What is best solution for form validation? Does React architecture/frameworks (Flux/Redux) provide any solution?

谢谢

推荐答案

我最近在React中使用了某些表单,我的经历也非常相似.我认为这归结为React是一个非常新的东西,而表单验证通常是一个很难解决的问题.这导致了形式验证的狂野西部,没有固定的标准,许多新的库试图解决该问题(每个库都以自己的方式进行并作了许多假设).

I worked with some forms in React recently, and I had a very similar experience. I think what this boils down to is that React is very new, and form validation is a hard problem to solve in general. This leads to kind of a Wild West of form validation, where there is no set standards, and many new libraries trying to solve the problem (each doing it their own way and making lots of assumptions).

我最终使用了formy-react( https://github.com/christianalfoni/formsy-react ),这很简单,但是做了一个假设-我们要验证输入的 onChange .我希望我的输入呈现错误 onBlur ,这导致我编写了一些辅助函数来创建该行为.

I ended up using formsy-react (https://github.com/christianalfoni/formsy-react) which was fairly straight-forward, but made one big assumption - that we want to validate the input onChange. I wanted my inputs to render an error onBlur which lead to me writing some helper functions to create that behavior.

我还没有深入研究Redux,但是在那个领域似乎确实有一些不错的选择( https://www.npmjs.com/package/redux-form ),可能会适合您的需求.

I haven't yet dug into Redux too much, but there do seem to be some good options in that arena (https://www.npmjs.com/package/redux-form), that may suit your needs.

让我知道您是否想看看我的Formsy表单验证器/帮助器方法的示例.

Let me know if you'd like to see an example of my Formsy form validators/helper methods.

希望这有助于或至少提供了一些支持,以至于到目前为止,React世界中的形式验证尚不清楚,并且缺乏标准.

Hope this helped, or at least provided some solidarity that form validation in the React world is, as of now, unclear and lacks a standard.

这篇关于反应:输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆