Skip to content

Commit

Permalink
map for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
OxCom committed Sep 9, 2020
1 parent 851a3ca commit 69a434b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/Field.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Field
A field object that represents input data field.

## Options

##### map_name
type: `string` default: `undefined`

Usually, if you have validation error it will be assigned to configured field. At some cases
you have to show this error on the different field.
This option used to specify the field name that should be used when error appears.

## Methods

##### ```addTransformer(transformer)```
Expand Down
3 changes: 1 addition & 2 deletions docs/Form.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ A form composed of fields, each can be validated by provided list of constraints

##### extra_fields
type: `boolean` default: `false`
type: boolean default: false

Usually, if you submit extra fields that aren't configured in your form, you'll get a
`"This form should not contain extra fields."` validation error.
Expand All @@ -29,7 +28,7 @@ Add field specification.
|---|---|---|
| field | string | The validation filed name |
| constants | array | List of constraints that should be assigned to the field |
| options | object | Extra options related to the field. Will be passed to each constraint |
| options | object | Extra options related to the [Field](./Field.md). Will be passed to each constraint |

**Return**: current ```Form```

Expand Down
16 changes: 14 additions & 2 deletions src/Validator/Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Field {
/**
* Filed configuration
*
* @type {{}}
* @type {{map_name: string}}
*/
options = {};

Expand All @@ -31,13 +31,25 @@ export default class Field {

/**
* @param {AbstractConstraint[]} [constants]
* @param {{}} [options]
* @param {{map_name: string}} [options]
*/
constructor(constants = [], options = {}) {
this.constraints = constants;
this.options = options;
}

/**
* Map field name if configured
*
* @param {string} name
* @return {string}
*/
getMappedFieldName(name) {
return typeof this.options.map_name === 'string' && this.options.map_name.length > 0
? this.options.map_name
: name;
}

/**
* @return {AbstractConstraint[]}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default class Form {
const errors = this.validator.validate(this.data[field], oField.getConstraints(), vOptions);

if (errors.length > 0) {
this.addValidationErrors(field, errors);
this.addValidationErrors(oField.getMappedFieldName(field), errors);
}

this.data[field] = pipe(oField.getReverseTransformers())(this.data[field], vOptions);
Expand Down
16 changes: 16 additions & 0 deletions tests/Validator/Form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,21 @@ describe('Form', function () {
assert.strictEqual(Object.keys(e).length, 0);
assert.strictEqual(data.email, 'email');
});

it('Field error can be mapped', function () {
const form = new Form();

form
.add('error_field')
.add('email', [
new NotBlank(),
new Email(),
], {map_name: 'error_field'});

const e = form.validate({email: 'email'});

assert.strictEqual(Object.keys(e).length, 1);
assert.strictEqual(e.error_field.join('; '), 'Error: This value is not valid email.');
});
});
});

0 comments on commit 69a434b

Please sign in to comment.