Tcomb-form-native: Is there a way to compare value with value of another field within refinement?

Created on 24 Jan 2017  路  4Comments  路  Source: gcanti/tcomb-form-native

Hi, guys!

Is there a way to keep this logic in refinement and not to do that via component state? would be great though.. like:

F.struct({
聽聽email: F.String,
聽聽newEmail: F.refinement(F.String, function(email) {
聽聽聽聽return email !== this.email.value
聽聽}),
})

Thanks!

Most helpful comment

I have done this. Take this code as an example:

    const PasswordMinLength = t.refinement(t.String, value => {
      return value.length > 6
    })
    const ConfirmPasswordEquality = t.refinement(t.String, value => {
      return value === this.state.value.password
    })
    this.SignupFormStruct = t.struct({
      name: t.String,
      middle_name: t.maybe(t.String),
      surname: t.String,
      birth_date: t.Dat,
      address: t.String,
      country_pid: t.enums(props.countries, 'Countries'),
      province_pid: t.maybe(t.String),
      city_pid: t.maybe(t.String),
      postcode: t.String,
      phone: t.String,
      email: t.String,
      password: PasswordMinLength,
      confirm_password: ConfirmPasswordEquality,
    })

The trick is to save the state of the form and use it in your refinements.

All 4 comments

I was wondering exactly the same. I have a simple sign up form with two fields password and passwordConfirmation and I'd like the second one to be marked in red on submit, (after calling this.refs.form.getValue()), if the passwords don't match.

I tried this:

function samePasswords (x) {
  return x.password === x.passwordConfirmation
}

const formType = t.refinement(t.struct({
  email: t.String,
  password: t.String,
  passwordConfirmation: t.String
}), samePasswords)

The validation works but it doesn't set the field as having an error.

Very interested if anyone has solved this yet.

I have done this. Take this code as an example:

    const PasswordMinLength = t.refinement(t.String, value => {
      return value.length > 6
    })
    const ConfirmPasswordEquality = t.refinement(t.String, value => {
      return value === this.state.value.password
    })
    this.SignupFormStruct = t.struct({
      name: t.String,
      middle_name: t.maybe(t.String),
      surname: t.String,
      birth_date: t.Dat,
      address: t.String,
      country_pid: t.enums(props.countries, 'Countries'),
      province_pid: t.maybe(t.String),
      city_pid: t.maybe(t.String),
      postcode: t.String,
      phone: t.String,
      email: t.String,
      password: PasswordMinLength,
      confirm_password: ConfirmPasswordEquality,
    })

The trick is to save the state of the form and use it in your refinements.

@gcanti thoughts on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sibelius picture sibelius  路  5Comments

timdoes picture timdoes  路  4Comments

sibelius picture sibelius  路  4Comments

dvlprmartins picture dvlprmartins  路  4Comments

abdelghafourzguindou picture abdelghafourzguindou  路  4Comments