HTML客户端表单验证,在使用JSON的React应用中似乎不起作用

时间:2019-05-06 11:48:08

标签: json reactjs

我正在用JSON编码一个简单的表单,该表单似乎无法正确验证。

这是组件实例:

  class App extends Component {

  state = {
    data: [
      {id: 1, name:"a", age:29, qualification:"B.Com", rating:3},
      {id: 2, name:"b", age:35, qualification:"B.Sc", rating:5},
      {id: 3, name:"c", age:42, qualification:"B.E", rating:3},
    ],
    current: {}
  }

  onSubmit = (model) => {
    model.id = +new Date();
    alert(JSON.stringify(model));
    this.setState({
      data: [model, ...this.state.data]
    })
  }

  render() {
    return (
      <div className="App">
        <DynamicForm className="form"
          title="Registration"
          model={[
            {key: "name", label: "Name", props: {required: true}},
            {key: "age",label: "Age", type: "number"},
            {key: "rating",label: "Rating", type: "number", props:{min:0,max:5}},
            {key: "qualification",label: "Qualification"},
          ]}
          onSubmit = {(model) => {this.onSubmit(model)}} 
        />

        <pre styles={{width:"300px"}}>
          {JSON.stringify(this.state.data)}
        </pre>

      </div>
    );
  }
}

export default App;

因此,如果您省略名称输入,则不会收到通知!

外面有人知道为什么吗?

将{required:true}添加到...

{键:“名称”,标签:“名称”,道具:{required:true}},

没有影响!

0 个答案:

没有答案