鍍金池/ 問答/HTML/ ant design如何自定義表單驗(yàn)證?

ant design如何自定義表單驗(yàn)證?

react ant如何使用自定義表單驗(yàn)證?

1.上代碼

const Login = (
  {
    loading,
    dispatch,
    form: {
      getFieldDecorator,
      validateFieldsAndScroll
    },
  }
  ) => {
  function handleOk() {
    validateFieldsAndScroll((errors, values) => {
      if (errors) {
        console.log(`錯(cuò)誤信息${errors}`);
        return
      }

      dispatch({type: 'login/login', payload: values}) //如果正確就發(fā)送至models進(jìn)行處理
    });
  }
  return (
    <div className={styles.form}>
      <div className={styles.logo}>
        <img alt="logo" src={logo_img}/>
        <span>Light of Hope</span>
      </div>
      <form>
        <FormItem hasFeedback>
          {getFieldDecorator('username', {
            rules: [
              {
                required: true,
                message:'請輸入您的用戶名'
              }
            ],
            // initialValue: 'admin', //初始值
          })(<Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)', fontSize:16 }} />} onPressEnter={handleOk} placeholder="用戶名"/>)}
        </FormItem>
        <FormItem hasFeedback>
          {getFieldDecorator('password', {
            rules: [
              {
                required: true,
                message:'請輸入您的密碼'
              },
              {
                validator(rule, values, callback){
                  if(rule&&values.length>0){
                    console.log('校驗(yàn)完成')
                  }else{
                    callback(
                      console.log('校驗(yàn)完成')
                    );
                  }
                }
              }
            ],
            // initialValue: '123456',//初始值
          })(<Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' , fontSize:16}} />}  type="password" onPressEnter={handleOk} placeholder="密碼"/>)}
        </FormItem>
        <Row>
          <Button type="primary" onClick={handleOk} loading={loading.effects.login}>
            登錄
          </Button>
          <p>
            <span>Username:admin</span>
            <span>Password:123456</span>
          </p>
        </Row>
      </form>
    </div>
  )
};

2.例如:我想驗(yàn)證密碼必須大于6位數(shù)并至少包含一個(gè)字母該如何進(jìn)行做呢?在網(wǎng)上查了下 說可以通過validator來實(shí)現(xiàn)自定義驗(yàn)證 但我作為一個(gè)react萌新 有點(diǎn)不太會(huì) 還請,各位能夠多多指點(diǎn)!

回答
編輯回答
浪蕩不羈

使用正則表達(dá)式

2017年3月21日 10:13
編輯回答
熟稔

這么簡單的,不需要validator.寫多個(gè)rule就可以了。

rules: [
              {
                required: true,
                message:'請輸入您的密碼'
              },
              {
                  lenght: 6,
                  message: 'xxxxxx'
              }
       ]

更多設(shè)置參考:async-validator

2018年6月4日 18:49