Mobx反应自定义DVR规则

时间:2018-07-19 10:40:41

标签: javascript reactjs mobx-react

我有以下自定义规则(https://foxhound87.github.io/mobx-react-form/docs/validation/modes/dvr-custom.html):

const rules: Object = {
    telephone: {
        function: (value: string) => value.match(/^\d{3}-\d{3}-\d{4}$/),
        message: 'The :attribute phone number is not in the format XXX-XXX-XXXX.'
    },

    password: {
        function: (value: string) => value.length > 7,
        getValue: value => value,
        message: 'boom boom boom'
    },

    confirmPassword: {
        function: (confirmPassword: string) => { console.log(rules.password.getValue()) },
        message: 'Passwords should be equal'
    }
};

它们的使用方式如下:

...
    {
        name: 'changePassword',
        label: 'Change your password',
        fields: [
            {
                name: 'password',
                label: t('user:New pasword'),
                rules: 'password',
                value: itemData.passowrd,
                type: 'password'
            },
            {
                name: 'confirmPassword',
                label: t('user:Confirm password'),
                rules: 'confirmPassword',
                value: itemData.confirmPassowrd,
                type: 'password'
            }
        ]
    },
...

,并且我尝试在ConfirmPassword规则内获取密码的值,但似乎无法完成我正在执行的工作。返回未定义。

1 个答案:

答案 0 :(得分:0)

如果您使用的是DVR插件,则只需应用same:attribute规则:

$ ./myProg abcd -h
usage: myProg abcd [-h] -p P [-x X] foo

positional arguments:
  foo

required arguments:
  -p P        help for -p

optional arguments:
  -h, --help  show this help message and exit
  -x X        help for -x

或者,您可以像这样使用custom validation function

{
  name: 'confirmPassword',
  label: t('user:Confirm password'),
  rules: 'same:password',
  value: itemData.confirmPassowrd,
  type: 'password'
}