@@ -2,6 +2,7 @@ import { isArray } from '../Utils/functions';
22import ViolationBuilder from '../Utils/ViolationBuilder' ;
33import Validator from './Validator' ;
44
5+ const FORM_ERROR_FIELD = 'form' ;
56const MESSAGE_EXTRA_FIELDS = 'This form should not contain extra fields.' ;
67
78export default class Form {
@@ -22,6 +23,7 @@ export default class Form {
2223 this . violationBuilder = new ViolationBuilder ( ) ;
2324 this . fields = { } ;
2425 this . data = { } ;
26+ this . errors = { } ;
2527 }
2628
2729 /**
@@ -65,50 +67,64 @@ export default class Form {
6567 * @return {Array }
6668 */
6769 validate ( data ) {
68- const errors = { } ;
69-
70+ this . errors = { } ;
7071 this . data = data ;
7172
7273 if ( ! this . options . extra_fields ) {
73- let extra = this . hasExtraFields ( data ) ;
74- if ( extra ) {
75- errors [ 'form' ] = [ extra ] ;
76- }
74+ this . checkExtraFields ( ) ;
7775 }
7876
79- if ( Object . keys ( errors ) . length > 0 ) {
80- return errors ;
77+ if ( Object . keys ( this . errors ) . length > 0 ) {
78+ return this . errors ;
8179 }
8280
83- Object . keys ( data ) . forEach ( ( field ) => {
84- const value = data [ field ] ;
81+ Object . keys ( this . fields ) . forEach ( ( field ) => {
82+ const value = this . data [ field ] ;
83+ const errors = this . validator . validate ( value , this . fields [ field ] . constants , { form : this } ) ;
8584
86- if ( typeof this . fields [ field ] === 'undefined' ) {
87- return true ;
85+ if ( errors . length > 0 ) {
86+ this . addValidationErrors ( field , errors ) ;
8887 }
88+ } ) ;
8989
90- const fieldErrors = this . validator . validate ( value , this . fields [ field ] . constants , { form : this } ) ;
90+ return this . errors ;
91+ }
9192
92- if ( fieldErrors . length > 0 ) {
93- errors [ field ] = fieldErrors ;
94- }
95- } ) ;
93+ /**
94+ * @param {string } field
95+ * @param {Error|Error[] } error
96+ * @return {Form }
97+ */
98+ addValidationErrors ( field , error ) {
99+ if ( ! isArray ( this . errors [ field ] ) ) {
100+ this . errors [ field ] = [ ] ;
101+ }
96102
97- return errors ;
103+ if ( isArray ( error ) ) {
104+ this . errors [ field ] = this . errors [ field ] . concat ( error ) ;
105+ } else {
106+ this . errors [ field ] . push ( error ) ;
107+ }
108+
109+ return this ;
98110 }
99111
100112 /**
101- * @param {Object } data
102- * @return {Error|undefined }
113+ * @return {Form }
103114 */
104- hasExtraFields ( data ) {
105- for ( const field of Object . keys ( data ) ) {
115+ checkExtraFields ( ) {
116+ for ( const field of Object . keys ( this . data ) ) {
106117 if ( typeof this . fields [ field ] === 'undefined' ) {
107- return this . violationBuilder . build ( this . options . extra_fields_message ) ;
118+ this . addValidationErrors (
119+ FORM_ERROR_FIELD ,
120+ this . violationBuilder . build ( this . options . extra_fields_message )
121+ ) ;
122+
123+ break ;
108124 }
109125 }
110126
111- return ;
127+ return this ;
112128 }
113129
114130 getData ( ) {
0 commit comments