如何在tcomb-form-native中使用secureTextEntry?

时间:2018-08-23 02:35:17

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

我正在使用t comb-form-native版本0.6.15在本机版本0.55.4中进行表单验证。当我使用密码字段时,文本应隐藏在圆点内。根据文档,我使用secureTextEntry并将其设置为true,但它仍像简单文本一样显示数据。我发现了一些使用神庙的建议,但我不知道如何使用它们。 下面是表单代码

const Form = t.form.Form;
t.form.Form.stylesheet.controlLabel.normal = {color: 'white'}
console.log(t.form.Form.options)
const User = t.struct({
    name: t.String,
    email: t.String,
    password: t.String,
});

const formStyles = {
    ...Form.stylesheet,
    formGroup: {
      normal: {
        marginBottom: 10,
        color: 'white'
      },
    },
    controlLabel: {
        normal: {
            fontSize: 12,
            marginBottom: 7,
            fontWeight: '600',
        },
        // the style applied when a validation error occours
        error: {
            color: 'red',
            fontSize: 18,
            marginBottom: 7,
            fontWeight: '600',
        },
    },
};

const options = {
    order: ['name', 'email', 'password' ],
    fields: {
        name: {
        placeholder: 'Enter Name',
        error: 'Name is empty?',
      },
      email: {
        placeholder: 'Enter Email',
        error: 'Email is empty?',
      },
      password: {
        placeholder: 'Enter Password',
        error: 'Password is empty?',
        secureTextEntry: true,
        template: (locals) => textbox(locals, )//here i can use template but don't know how 
      },
    },
    stylesheet: formStyles,
};

class SignupScreen extends Component{

    static navigationOptions = {
        header: null
    };

    constructor(props) {
        super(props);
        this.state = {
            name: '',
            email: '',
            password: '',
            dateOfBirth: '',
        };
    }
    onSubmit(){
        const value = this._form.getValue();
        console.log('value: ', value);
    }

    render(){
        console.log(this.state.dateOfBirth)
        return(
            <ImageBackground
                source={require('../../images/splash_screen.jpg')}
                style={{flex: 1}}
            >
                <View style={styles.container}>
                    <View style={{flex:3, justifyContent: 'center', alignItems: 'center'}}>
                        <Image source={require('../../images/big_transparent_logo.png')} />
                        <Text style={styles.subtitleStyle}>Get free drink everyday</Text>
                    </View>
                    <View style={{ flex: 7, justifyContent: 'flex-start', alignSelf: 'center', alignItems: 'center', width: '80%'}}>
                        <View style={{width: '100%', paddingHorizontal: 10, paddingVertical: 10 , backgroundColor: 'rgba(0,0,0,0.6)'}}>
                            <Form ref={c => (this._form = c)} type={User} />
                            <TouchableOpacity  style={{width: '100%', marginVertical: 10}} >
                                <Label title="BIRTHDAY" />
                                <DatePicker
                                    style={{width: '100%'}}
                                    date=''
                                    mode="date"
                                    placeholder={this.state.dateOfBirth}
                                    format="DD-MM-YYYY"
                                    maxDate="2002-06-01"
                                    confirmBtnText="Confirm"
                                    cancelBtnText="Cancel"
                                    showIcon={true}
                                    androidMode='spinner'
                                    customStyles={{
                                    dateInput: {
                                        marginLeft: 0,
                                        borderWidth: 0,
                                        textAlign: 'left',
                                        color: 'white', 
                                        backgroundColor: '#f2f2f2', 
                                        paddingVertical: 0, 
                                        height: 30,
                                        color: 'black'
                                    }
                                    // ... You can check the source to find the other keys.
                                    }}
                                    onDateChange={(date) => {this.setState({dateOfBirth: date})}}
                                />
                            </TouchableOpacity >
                            <Button block bordered light  
                                style={{ width: '47%', borderRadius: 5, alignSelf: 'center', height: 50}}
                                onPress={this.onSubmit.bind(this)}
                            >
                                <Text style={{color: 'white'}}>Sign Up</Text>
                            </Button>
                            <Button block light 
                                style={{ width: '47%', borderRadius: 5, alignSelf: 'center', height: 50, backgroundColor: 'rgba(0,0,0,0)',}}
                                onPress={()=>this.props.navigation.goBack()}
                            >
                                <Text style={{color: 'white'}}>Back</Text>
                            </Button>
                        </View>
                    </View>
                </View>
                
            </ImageBackground>
        );
    }
}

export { SignupScreen };

1 个答案:

答案 0 :(得分:2)

您的表单未连接到options变量。表单标签中的调用选项,如下所示。

<Form ref={c => (this._form = c)} 
      type={User} 
      options={options} //set form options
/>

它应该没有模板就可以工作。

password: {
      password: true,
      secureTextEntry: true}
   }