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.

How can set a custom message for a field?

See original GitHub issue

Im already use your example code to set my message on spanish: this.validator = new SimpleReactValidator({ messages: { email: ‘That is not an email.’ // OR default: ‘Validation has failed!’ // will override all messages }, }

but Im still getting the message on english, , here’s my code:

constructor(props) {
    super(props);
    this.state = {
      name: '',
    };

   this.validator = new Validator({
      element: message => (
        <span style={error}>
          <i className="fa fa-exclamation-circle" aria-hidden="true" />
          { ` ${message}`}
        </span>
      ),
      messages: {
        name: 'El campo nombre es requerido',
      },
    });

<div className="form-group">
 <label htmlFor="name">Nombre: *</label>
 <input
 type="text"
 name="name"
 onChange={this.onChangeHandle}
 className="form-control"
 id="name"
 placeholder="Nombre Campaña / Cupón"
 />
 {this.validator.message('name', this.state.name, 'required|alpha')}
 </div>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
stuyamcommented, Feb 25, 2019

I see the problem, the key for the message should be the validator rule name you want it to show on not the attribute of field (aka ‘alpha’ not 'name). For example it should look like this:

messages: {
  alpha: 'El campo nombre es requerido',
},

and you could do this to make it dynamic:

messages: {
  alpha: 'El campo :attribute es requerido',
},
.....
{this.validator.message('nombre', this.state.name, 'required|alpha')}

Let me know if you have any questions! Enjoy 👍

0reactions
BSThisarasinghecommented, Oct 30, 2020

Oh now it’s clear. Thank you sso much.

On Thu, Oct 15, 2020 at 7:07 PM Stuart Yamartino notifications@github.com wrote:

@BSThisarasinghe https://github.com/BSThisarasinghe you can do it in the initializer options for the whole component or inline like this:

this.validator = new SimpleReactValidator({ messages: { required: ‘This field is required’, email: ‘Some message’ } }); // OR

{this.validator.message(‘email’, this.state.name, ‘required|email’), {messages: {required: ‘This field is required’, email: ‘Some message’}} }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dockwa/simple-react-validator/issues/55#issuecomment-709330066, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQPQZX3UZNOZU4L3DCYDVDSK33I7ANCNFSM4G2CMDJA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Personalizing Messages Using Custom Fields
Adding Custom Fields to Messages ... 1 Go to Campaigns and click New campaign. 2. Start typing out your message and click the...
Read more >
Create a custom field for Outlook messages - Slipstick Systems
Create a custom field for Outlook messages · Open the View Settings dialog from the View ribbon. · Click Columns to open the...
Read more >
Adding and Sending Messages with Custom Fields - YouTube
Custom fields are a perfect way to personalize your messages. You can add your own custom fields into your messages using these simple...
Read more >
Is there a way to create a custom error message for one field?
From what I get from previous suggestion by our colleague, you can use the TEXT field to create your custom error message for...
Read more >
How to add a custom validation message for the required field?
If you want to set one message for all the required fields across the available forms, you can use this option. Go to...
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