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.

Passing argument to the Field validation?

See original GitHub issue

I am wondering if you could pass arguments to the Field level validation like so:

<Field name="username" type="text"
	component={renderField} label="Username"
	validate={[ required('an argument to this function'), maxLength15 ]}
/>

// The validator
const required = (value, message = 'This is required') => value ? undefined : message;

This would make it easy to customize messages based on the field, without needing to create a validation function which will repeat a lot of code just to have a suiting message.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

28reactions
erikrascommented, Jan 13, 2017

⚠️ WARNING ⚠️

Beware of doing this:

<Field validate={[isRequired('an argument to this function'), maxLength(15)]}/> ❌

As tempting and elegant as it looks, this will construct a new function every time your form is rendered, which will cause your field to rerender (because this.props.validate !== nextProps.validate).

This is why I specifically defined a single instance of my parameterized validation rules in the example:

const maxLength = max => value =>
  value && value.length > max ? `Must be ${max} characters or less` : undefined
const maxLength15 = maxLength(15) ✅
...
<Field name="username" type="text"
  component={renderField} label="Username"
  validate={[ required, maxLength15 ]}/> ✅
6reactions
aniruddhashevlecommented, Dec 9, 2017

I’ve found the solution for this…

let validationObj = {
    required: true,
    type: 'number'
}

<Field
    name='first_name'
    type='text'
    className="input form-flow"
    component={RenderTextField}
    placeholder={field.label}
    validate={[
         (value, allValues, props, name) => {
               let errors = dynamicValidator(value, validationObj);
               return errors;
          }
    ]}
/>
dynamicValidator = (value, validationObj) => {
    //required validation
    if (validationObj.required === true) {
      if (!value) {
        return 'Required'; //Error msg
      }
    }

    //number validation
    if (validationObj.type === 'number') {
      if (isNaN(Number(value))) {
        return 'Must be a number'; //Error msg
      }
    }

     /* And so on... */
   
}

The above implementation shows me the error msg properly for the respective field.

@erikras : Please let me know your thoughts on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django - Passing an argument from views to Form Validation
Is there a way I can get the product gotten from product = Product.objects.get(id=id) and use it in my form validation?
Read more >
passing arguments to the validation - WordPress.org
I just need to pass the post_id using a form argument. I defined the form argument while loading the form add_filter('acfe/form/load/form=prop_edit', ' ...
Read more >
How to pass parameters to a custom validator in Sitecore
I have a custom validator class, to validate a General Link field. It checks that the field value of the Description field is...
Read more >
Function Argument Validation - MATLAB & Simulink
Function argument validation is a way to declare specific restrictions on function arguments. Using argument validation you can constrain the class, size, and ......
Read more >
How to Pass Argument to the Required Field Validator
So pls provide me the solution how can i pass the parameter of id to required field validator. Thanks. Posted 9-Sep-13 0:27am.
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