From 69a434bf7b0737b733efee7176399e9024dea074 Mon Sep 17 00:00:00 2001 From: oxcom Date: Wed, 9 Sep 2020 15:48:42 +0200 Subject: [PATCH] map for errors --- docs/Field.md | 9 +++++++++ docs/Form.md | 3 +-- src/Validator/Field.js | 16 ++++++++++++++-- src/Validator/Form.js | 2 +- tests/Validator/Form.spec.js | 16 ++++++++++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docs/Field.md b/docs/Field.md index b5b114b..758745e 100644 --- a/docs/Field.md +++ b/docs/Field.md @@ -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)``` diff --git a/docs/Form.md b/docs/Form.md index f8f3187..d1decaf 100644 --- a/docs/Form.md +++ b/docs/Form.md @@ -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. @@ -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``` diff --git a/src/Validator/Field.js b/src/Validator/Field.js index 26296ba..f3e7e66 100644 --- a/src/Validator/Field.js +++ b/src/Validator/Field.js @@ -11,7 +11,7 @@ export default class Field { /** * Filed configuration * - * @type {{}} + * @type {{map_name: string}} */ options = {}; @@ -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[]} */ diff --git a/src/Validator/Form.js b/src/Validator/Form.js index cec2394..7fe0da2 100644 --- a/src/Validator/Form.js +++ b/src/Validator/Form.js @@ -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); diff --git a/tests/Validator/Form.spec.js b/tests/Validator/Form.spec.js index 8bdcf5c..7f8525e 100644 --- a/tests/Validator/Form.spec.js +++ b/tests/Validator/Form.spec.js @@ -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.'); + }); }); });