rails_errors

0.0.3 • Public • Published

rails_errors

Very basic npm package to parse Rails errors and help building forms. Works nicely with React Bootstrap.

Installation

npm install --save rails_errors

Usage

Assuming a JSON response like this:

{ "errors": { "some_field": ["is invalid"] } }

You can write code like this:

const railsErrors = new RailsErrors(jsonResponse.errors)

railsErrors.forField('some_field') // => "is invalid"

railsErrors.firstMessage // => "is invalid"

railsErrors.fields // => "some_field"

And write some React Bootstrap components with proper validation errors:

function MyInput({ label, name, railsErrors, ...props }) {
  const errorMsg = railsErrors.forField(name);
  
  return (
    <FormControl controlId={name} validationState={errorMsg && 'error'}>
      <ControlLabel>{label}</ControlLabel>
      <FormControl {...props}/>
      <HelpBlock>{errorMsg}</HelpBlock>
    </FormControl>
  );
}

Future development

  • Find out how to properly display the field name, so we can display full error messages.

Readme

Keywords

Package Sidebar

Install

npm i rails_errors

Weekly Downloads

2

Version

0.0.3

License

MIT

Last publish

Collaborators

  • fabiob