New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form model addRule() data property undefined #288

Closed
lmovse opened this Issue Nov 29, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@lmovse

lmovse commented Nov 29, 2018

Bug, feature request, or proposal:

Bug

What is the expected behavior?

当我为某一 form 字段的校验添加自定义规则时,应该可以读取 data 属性,然后根据 data 里面的其他字段的值动态判断当前字段的值是否合法,下面的代码摘自官方文档 Form 里的一个 CheckForm 的例子

const model = Schema.Model({
  name: StringType().isRequired('This field is required.'),
  email: StringType()
    .isEmail('Please enter a valid email address.')
    .isRequired('This field is required.'),
  age: NumberType('Please enter a valid number.').range(
    18,
    30,
    'Please enter a number from 18 to 30'
  ),
  password: StringType().isRequired('This field is required.'),
  verifyPassword: StringType()
    .addRule((value, data) => {
      console.log(data);

      if (value !== data.password) {
        return false;
      }

      return true;
    }, 'The two passwords do not match')
    .isRequired('This field is required.')
});

What is the current behavior?

获取不到 addRule((value, data) => ..., '') 里的 data 的值,提示该值为 undefined

What are the steps to reproduce?

将官方文档里的 Form 一节的表单数据校验小节里面的 CheckForm 组件 copy 到项目里,然后渲染该组件,在 verifyPassword 一栏输入时会直接报错,报错如下

CheckForm.js:30 Uncaught TypeError: Cannot read property 'password' of undefined
    at CheckForm.js:30
    at StringType.check (Type.js:54)
    at Schema.checkForField (Schema.js:32)
    at DefaultPropsComponent.handleFieldCheck (FormControl.js:183)
    at handleFieldChange (FormControl.js:161)
@hypers-bot

This comment has been minimized.

hypers-bot commented Nov 29, 2018

Translation of this issue:

Bug, feature request, or proposal:

Bug

What is the expected behavior?

When I add a custom rule for the validation of a form field, I should be able to read the data property and then dynamically determine whether the value of the current field is valid based on the values ​​of other fields in the data. The following code is taken from the official document Form. An example of a CheckForm

Const model = Schema.Model({
  Name: StringType().isRequired('This field is required.'),
  Email: StringType()
    .isEmail('Please enter a valid email address.')
    .isRequired('This field is required.'),
  Age: NumberType('Please enter a valid number.').range(
    18,
    30,
    'Please enter a number from 18 to 30'
  ),
  Password: StringType().isRequired('This field is required.'),
  verifyPassword: StringType()
    .addRule((value, data) => {
      Console.log(data);

      If (value !== data.password) {
        Return false;
      }

      Return true;
    }, 'The two passwords do not match')
    .isRequired('This field is required.')
});

What is the current behavior?

Can't get the value of data in addRule((value, data) => ..., ''), suggesting that the value is undefined

What are the steps to reproduce?

Copy the CheckForm component in the Form Data Validation section of the Form section of the official document into the project, and then render the component. When you type in the verifyPassword column, you will report an error directly. The error is as follows:

CheckForm.js:30 Uncaught TypeError: Cannot read property 'password' of undefined
    At CheckForm.js:30
    At StringType.check (Type.js:54)
    At Schema.checkForField (Schema.js:32)
    At DefaultPropsComponent.handleFieldCheck (FormControl.js:183)
    At handleFieldChange (FormControl.js:161)
@simonguo

This comment has been minimized.

Member

simonguo commented Nov 29, 2018

@lmovse 你的版本是?

@lmovse

This comment has been minimized.

lmovse commented Nov 29, 2018

@lmovse 你的版本是?

3.2.1

@simonguo

This comment has been minimized.

Member

simonguo commented Nov 29, 2018

这个功能是在 3.2.8 加入的。
建议你升级 rsuite 到最新版本

@lmovse

This comment has been minimized.

lmovse commented Nov 29, 2018

OK,可以了.....抱歉,是我的疏忽,麻烦你了

@lmovse lmovse closed this Nov 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment