question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for rules/related validation with Array path.

See original GitHub issue

I’m not sure if I’m doing something wrong, but I’m trying to make validation work with an array of objects where 2 properties are related to each other, e.g. at least one needs to be present.

In the demo below: members[].id should be required unless the members[].age is present. At least that’s the goal, am I doing something wrong or is there a bug in the lib?

Link to the demo

Lib versions:

mobx: 3.3.0 mobx-react-form: 1.32.2 validatorjs: 3.13.5

Output

is form Valid: false
form.values:
{
  "club": {
    "name": "Happy club",
    "city": "New York"
  },
  "members": [{
    "lastname": "Doe",
    "firstname": "John",
    "age": 32,
    "id": ""
  }]
}

errors:
{
  "club": {
    "name": null,
    "city": null
  },
  "members": [{
    "lastname": null,
    "firstname": null,
    "age": null,
    "id": "The id field is required when members[].age is empty."
  }]
}

Code for the demo

import Validator from 'validatorjs';
import MobxReactForm from 'mobx-react-form';

const fields = [
  'club.name',
  'club.city',
  'members',
  'members[].firstname',
  'members[].lastname',
  'members[].age',
  'members[].id'
];


const rules = {
  'club.name': 'string|required|min:3',
  'club.city': 'string|required|min:3',
  'members[].lastname': 'string|required',
  'members[].firstname': 'string|required',
  'members[].age': 'required|integer',
  'members[].id': 'boolean|required_without:members[].age'
};

var values = {
    club: {
      name: 'Happy club',
      city: 'New York'
    },
    members: [{
      lastname: 'Doe',
      firstname: 'John',
      age: 32,
      id: null
    }]
};

function report(form) {
    console.log('is form Valid: ', form.isValid);
    console.log('form.values: ',  JSON.stringify(form.values(), null, 2));
    console.log('errors: ', JSON.stringify(form.errors(), null, 2));
}

class NestedFieldsForm extends MobxReactForm {
  plugins() {
    return {dvr: Validator}
  }
  setup() {
     return {fields, values, rules}
  }
}

const form = new NestedFieldsForm();

form.submit({
  onError: form => report(form),
  onSuccess: form => report(form)
});

Edit: code format fix

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
simon998yangcommented, Nov 15, 2018

It’s been one year , any progress on this issue? ta

1reaction
foxhound87commented, Sep 28, 2017

Right now using the array notation in the rules is not supported, you should specify the index of the array to validate inside the rule, so I suggest to dynamically create the rules:

const makeMemberIdRules = (id) =>   
  'boolean|required_without:members[' + id + '].age';

form.$('members')
  .each((field) => (field.name === 'id') && 
    field.set('rules', makeMemberIdRules(field.container().name)));

I updated your demo: https://www.webpackbin.com/bins/-Kv7y24BahF4zIdxa1nv

I will implement this feature in the future versions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validating arrays and nested values in Laravel - LogRocket Blog
Luckily, Laravel provides a simple way to validate arrays and nested array input with the use of dot notation and the * character....
Read more >
Validators - Angular
A validator is a function that processes a FormControl or collection of ... in case of an empty string or an empty array),...
Read more >
Define Validation Rules - Salesforce Help
A validation rule can contain a formula or expression that evaluates the data in one or more fields and ... In the Validation...
Read more >
Validating an array on complex paths - symfony - Stack Overflow
What I want to do is create DTO-like objects that are validated and then passed to the controller. That part works generally fine,...
Read more >
Python - Loop through files of certain extensions
Path containing different files: This will be used for all methods. ... match specified patterns according to rules related to Unix shell.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found