【问题标题】:Validate string equality with tcomb-form-native (confirm password)使用 tcomb-form-native 验证字符串是否相等(确认密码)
【发布时间】:2017-09-27 11:24:26
【问题描述】:

如何使用 tcomb-form-native 库验证我的 confirmPassword 字段?

电子邮件和密码字段非常琐碎,但我不知道如何与模型中另一个字段的现有值进行比较。

这是我的代码。

const Email = t.refinement(t.String, (email) => {
  const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
  return reg.test(email);
});

const Password = t.refinement(t.String, (password) => {
  const reg = /^(?=\S+$).{8,}$/;
  return reg.test(password);
});

const RegistrationData = t.struct({
  email: Email,
  password: Password,
  confirmPassword: t.String // Need some equality check
});

我已经调查了文档 https://github.com/gcanti/tcomb-form-native#disable-a-field-based-on-another-fields-value,但我无法理解。

【问题讨论】:

  • Afaik,您无法通过验证器执行此操作。相反,您处理对表单提交的检查。
  • 嗯,真可惜。我觉得对此进行实时验证会是更好的用户体验。
  • 好吧,你总是可以自己写:) onTextChange => 持久化文本;验证();

标签: validation react-native tcomb-form-native


【解决方案1】:

这是我的解决方案:

首先,在类构造函数this.samePassword 中定义一个用于密码检查的类型。

this.samePassword = t.refinement(t.String, (s) => {
    return s == this.state.person.user_password;
});

然后,在表单定义中使用this.samePassword 类型

this.Person = t.struct({
    user_id:          t.String,
    user_password:    t.String,
    reenter_password: this.samePassword,
});

接下来,准备一个onChange 函数来处理文本更改并保存到状态。 this.validate 是一个变量,表示表单是否被输入。

onChange(person) {
    this.setState({ person });
    if(person.reenter_password != null && person.reenter_password != "") {
        this.validate = this.refs.form.getValue();
    }
}

最后,钩住this.statethis.onChange... 到<Form>

<Form
    ref="form"
    type={this.Person}
    value={this.state.person}
    onChange={(v) => this.onChange(v)}
    options={this.options}
/> 

完整代码如下:

import React from "react";
import {View, TouchableOpacity, Text} from "react-native";
import * as t from "tcomb-form-native";

let Form = t.form.Form;

export default class CreateUser extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            person: {}
        };

        this.samePassword = t.refinement(t.String, (s) => {
            return s == this.state.person.user_password;
        })
        this.Person = t.struct({
            user_id:          t.String,
            user_password:    t.String,
            reenter_password: this.samePassword,
        });
        this.options = {
            fields: {
                user_password: {
                    password: true,
                    secureTextEntry: true,
                    error: "",
                },
                reenter_password: {
                    password: true,
                    secureTextEntry: true,
                    error: "different password",
                },
            }
        };
        this.validate = null;
    }
    onChange(person) {
        this.setState({ person });
        if(person.reenter_password != null && person.reenter_password != "") {
            this.validate = this.refs.form.getValue();
        }
    }


    render() {
        return (
            <View>
                <Form
                    ref="form"
                    type={this.Person}
                    value={this.state.person}
                    onChange={(v) => this.onChange(v)}
                    options={this.options}
                />
                <View>
                    <TouchableOpacity
                        style={{backgroundColor: this.validate ? "blue": "red"}}
                        activeOpacity={this.validate ? 0.5 : 1}
                        disabled={this.validate? false: true}
                        onPress={() => this.doNext()}>
                        <Text> NEXT MOVE </Text>
                    </TouchableOpacity>
                </View>
            </View>
        );
    }
}

希望这会有所帮助!

【讨论】:

  • 谢谢,这是一个很好的解决方案,但是它假定验证改进与组件共享范围(您将字符串 s 与组件的范围 this.state.person 进行比较)。就我而言,我的验证规则不是使用它的组件的一部分,而是抽象到一个库中。我正在考虑对其进行重组以在组件中使用相等检查方法,因为此时我无法调整细化本身。
  • 也许我们需要改进它而不验证整个表单,而只验证字段
猜你喜欢
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2011-09-18
  • 2017-04-13
相关资源
最近更新 更多