Skip to content
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

Ajax validation rule. #261

Open
minvs1 opened this issue Nov 12, 2015 · 4 comments
Open

Ajax validation rule. #261

minvs1 opened this issue Nov 12, 2015 · 4 comments

Comments

@minvs1
Copy link

minvs1 commented Nov 12, 2015

I want to add validation rule to check if value is unique in database. So basically I just post request to back-end and wait till i get response if value is unique or not. Also I use underscore.js's debounce function to reduce calls to server. But input is being invalidated before ajax callback comes.

Code looks something like this:

Formsy.addValidationRule 'isUnique', (values, value)->
  return checkUnique(values, value)

checkUnique = _.debounce((values, value)=>
  unique = false

  request = $.ajax url: '/uniqueness-check', method: 'POST', data: {field: 'PaymentGateway.name', value: value}, async: false, dataType: 'json'

  request.done (response)->
    if response.status
      unique = true

  request.always ->
    console.log 'callback'

  return unique
, 300)

I tried many different version, but nothing seems to work. I also tried to validate input in Input component. I just accessed validations props and checked if it had isUnique parameter and validated it - errors = uniqueCheckErrors || this.getErrorMessage()., but I could figured out how to add error to Formsy error stack(so I could get them through this.getErrorMessage()) so form would act as normal.

@minvs1
Copy link
Author

minvs1 commented Nov 12, 2015

The perfect solution I think would allow to invalidate input like this:

Formsy.addValidationRule 'isUnique', (inputRef, values, value)->
  return checkUnique(inputRef, values, value)

checkUnique = _.debounce((inputRef, values, value)=>
  unique = false

  request = $.ajax url: '/uniqueness-check', method: 'POST', data: {field: 'PaymentGateway.name', value: value}, dataType: 'json'

  request.done (response)->
    if response.status
      unique = true

  unless unique
    inputRef.invalidate()
, 300)

@kelly-kellerheikkila
Copy link

+1

It would be great to arbitrarily declare an input as invalid.

@jcrjaci
Copy link

jcrjaci commented Oct 18, 2017

For anyone who need to validate some field with an AJAX request here are an work around example:

this.validateName = _.debounce(this.validateName.bind(this), 500);

validateName(field, value) {
    const state = {};

    $.when(this.serverValidation(value)).then((validation) => {
        state.nameIsValid = validation;
    }).catch((validation) => {
        state.nameIsValid = validation;
    })
    .done(() => {
        state.name = value;
        this.setState(state);
    });
}

serverValidation(name) {
// make your server request and resolve the validation
}

<MyInput
    name={this.state.name}
    onChange={this.validateName}
    validations={{
        nameIsValid: () => this.state.nameIsValid,
    }}
>

@yantakus
Copy link

yantakus commented Oct 12, 2018

@jcrjaci When I try using your solution, I get the following error: Formsy does not allow you to override default validations. It seems this way of doing validation was deprecated:

formsy-react/src/main.js

Lines 311 to 313 in f7cf41d

if (validationRules[validationMethod] && typeof validations[validationMethod] === 'function') {
throw new Error('Formsy does not allow you to override default validations: ' + validationMethod);
}

If you pass a function as validator it throws an error.

Is there another way of doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants