@bombillazo/rhf-plus 7.71.2-plus.4 → 7.72.0-plus.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAaV,WAAW,EACX,YAAY,EAiBZ,YAAY,EAIZ,aAAa,EAUd,MAAM,UAAU,CAAC;AAyDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EACjC,SAAS,SAAS,YAAY,GAAG,GAAG,EAEpC,KAAK,GAAE,YAAY,CACjB,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,SAAS,CACL,GACL,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,CAAC;CACH,CAo0DA"}
1
+ {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAaV,WAAW,EACX,YAAY,EAiBZ,YAAY,EAIZ,aAAa,EAWd,MAAM,UAAU,CAAC;AA0DlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EACjC,SAAS,SAAS,YAAY,GAAG,GAAG,EAEpC,KAAK,GAAE,YAAY,CACjB,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,SAAS,CACL,GACL,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,CAAC;CACH,CAw6DA"}
@@ -1 +1 @@
1
- {"version":3,"file":"updateFieldArrayRootError.d.ts","sourceRoot":"","sources":["../../src/logic/updateFieldArrayRootError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,iBAAiB,EAClB,MAAM,UAAU,CAAC;yBAKF,CAAC,SAAS,WAAW,GAAG,WAAW,EACjD,QAAQ,WAAW,CAAC,CAAC,CAAC,EACtB,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,EAC1C,MAAM,iBAAiB,KACtB,WAAW,CAAC,CAAC,CAAC;AAJjB,wBASE"}
1
+ {"version":3,"file":"updateFieldArrayRootError.d.ts","sourceRoot":"","sources":["../../src/logic/updateFieldArrayRootError.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,iBAAiB,EAClB,MAAM,UAAU,CAAC;yBAKF,CAAC,SAAS,WAAW,GAAG,WAAW,EACjD,QAAQ,WAAW,CAAC,CAAC,CAAC,EACtB,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,EAC1C,MAAM,iBAAiB,KACtB,WAAW,CAAC,CAAC,CAAC;AAJjB,wBASE"}
@@ -14,7 +14,11 @@ const EVENTS = {
14
14
  BLUR: 'blur',
15
15
  FOCUS_OUT: 'focusout',
16
16
  FOCUS: 'focus',
17
- FOCUS_IN: 'focusin'};
17
+ FOCUS_IN: 'focusin',
18
+ SUBMIT: 'submit',
19
+ TRIGGER: 'trigger',
20
+ VALID: 'valid',
21
+ };
18
22
  const VALIDATION_MODE = {
19
23
  onBlur: 'onBlur',
20
24
  onChange: 'onChange',
@@ -31,6 +35,8 @@ const INPUT_VALIDATION_RULES = {
31
35
  required: 'required',
32
36
  validate: 'validate',
33
37
  };
38
+ const FORM_ERROR_TYPE = 'form';
39
+ const ROOT_ERROR_TYPE = 'root';
34
40
 
35
41
  var isDateObject = (value) => value instanceof Date;
36
42
 
@@ -447,6 +453,8 @@ function getFieldValue(_f) {
447
453
  return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
448
454
  }
449
455
 
456
+ var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
457
+
450
458
  var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
451
459
  const fields = {};
452
460
  for (const name of fieldsNames) {
@@ -498,8 +506,6 @@ var hasValidation = (options) => options.mount &&
498
506
  options.pattern ||
499
507
  options.validate);
500
508
 
501
- var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
502
-
503
509
  var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
504
510
 
505
511
  var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
@@ -633,7 +639,7 @@ var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(re
633
639
 
634
640
  var updateFieldArrayRootError = (errors, error, name) => {
635
641
  const fieldArrayErrors = convertToArrayPayload(get(errors, name));
636
- set(fieldArrayErrors, 'root', error[name]);
642
+ set(fieldArrayErrors, ROOT_ERROR_TYPE, error[name]);
637
643
  set(errors, name, fieldArrayErrors);
638
644
  return errors;
639
645
  };
@@ -937,7 +943,11 @@ function createFormControl(props = {}) {
937
943
  _updateIsValidating();
938
944
  }
939
945
  else {
940
- isValid = await executeBuiltInValidation(_fields, true);
946
+ isValid = await executeBuiltInValidation({
947
+ fields: _fields,
948
+ onlyCheckValid: true,
949
+ eventType: EVENTS.VALID,
950
+ });
941
951
  }
942
952
  if (isValid !== _formState.isValid) {
943
953
  _subjects.state.next({
@@ -986,7 +996,9 @@ function createFormControl(props = {}) {
986
996
  shouldSetValues && set(_formState.touchedFields, name, touchedFields);
987
997
  }
988
998
  if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
989
- _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
999
+ const fullDirtyFields = getDirtyFields(_defaultValues, _formValues);
1000
+ const rootName = getNodeParentName(name);
1001
+ set(_formState.dirtyFields, rootName, get(fullDirtyFields, rootName));
990
1002
  }
991
1003
  _subjects.state.next({
992
1004
  name,
@@ -1133,8 +1145,7 @@ function createFormControl(props = {}) {
1133
1145
  };
1134
1146
  const _runSchema = async (name) => {
1135
1147
  _updateIsValidating(name, true);
1136
- const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1137
- return result;
1148
+ return await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1138
1149
  };
1139
1150
  const executeSchemaAndUpdateState = async (names) => {
1140
1151
  const { errors } = await _runSchema(names);
@@ -1152,9 +1163,55 @@ function createFormControl(props = {}) {
1152
1163
  }
1153
1164
  return errors;
1154
1165
  };
1155
- const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
1166
+ const validateForm = async ({ name, eventType, }) => {
1167
+ if (props.validate) {
1168
+ const result = await props.validate({
1169
+ formValues: _formValues,
1170
+ formState: _formState,
1171
+ name,
1172
+ eventType,
1173
+ });
1174
+ if (isObject(result)) {
1175
+ for (const key in result) {
1176
+ const error = result[key];
1177
+ if (error) {
1178
+ setError(`${FORM_ERROR_TYPE}.${key}`, {
1179
+ message: isString(result.message) ? result.message : '',
1180
+ type: INPUT_VALIDATION_RULES.validate,
1181
+ });
1182
+ }
1183
+ }
1184
+ }
1185
+ else if (isString(result) || !result) {
1186
+ setError(FORM_ERROR_TYPE, {
1187
+ message: result || '',
1188
+ type: INPUT_VALIDATION_RULES.validate,
1189
+ });
1190
+ }
1191
+ else {
1192
+ clearErrors(FORM_ERROR_TYPE);
1193
+ }
1194
+ return result;
1195
+ }
1196
+ return true;
1197
+ };
1198
+ const executeBuiltInValidation = async ({ fields, onlyCheckValid, name, eventType, context = {
1156
1199
  valid: true,
1157
- }) => {
1200
+ runRootValidation: false,
1201
+ }, }) => {
1202
+ if (props.validate) {
1203
+ context.runRootValidation = true;
1204
+ const result = await validateForm({
1205
+ name,
1206
+ eventType,
1207
+ });
1208
+ if (!result) {
1209
+ context.valid = false;
1210
+ if (onlyCheckValid) {
1211
+ return context.valid;
1212
+ }
1213
+ }
1214
+ }
1158
1215
  for (const name in fields) {
1159
1216
  const field = fields[name];
1160
1217
  if (field) {
@@ -1170,25 +1227,34 @@ function createFormControl(props = {}) {
1170
1227
  ..._names.disabled,
1171
1228
  ..._names.readonly,
1172
1229
  ]);
1173
- const fieldError = await validateField(field, skipValidationFields, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1230
+ const fieldError = await validateField(field, skipValidationFields, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !onlyCheckValid, isFieldArrayRoot);
1174
1231
  if (isPromiseFunction && _proxyFormState.validatingFields) {
1175
1232
  _updateIsValidating([_f.name]);
1176
1233
  }
1177
1234
  if (fieldError[_f.name]) {
1178
1235
  context.valid = false;
1179
- if (shouldOnlyCheckValid || props.shouldUseNativeValidation) {
1236
+ if (onlyCheckValid) {
1180
1237
  break;
1181
1238
  }
1182
1239
  }
1183
- !shouldOnlyCheckValid &&
1240
+ !onlyCheckValid &&
1184
1241
  (get(fieldError, _f.name)
1185
1242
  ? isFieldArrayRoot
1186
1243
  ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
1187
1244
  : set(_formState.errors, _f.name, fieldError[_f.name])
1188
1245
  : unset(_formState.errors, _f.name));
1246
+ if (props.shouldUseNativeValidation && fieldError[_f.name]) {
1247
+ break;
1248
+ }
1189
1249
  }
1190
1250
  !isEmptyObject(fieldValue) &&
1191
- (await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
1251
+ (await executeBuiltInValidation({
1252
+ context,
1253
+ onlyCheckValid,
1254
+ fields: fieldValue,
1255
+ name: name,
1256
+ eventType,
1257
+ }));
1192
1258
  }
1193
1259
  }
1194
1260
  return context.valid;
@@ -1432,6 +1498,7 @@ function createFormControl(props = {}) {
1432
1498
  const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
1433
1499
  const isFocusEvent = event.type === EVENTS.FOCUS || event.type === EVENTS.FOCUS_IN;
1434
1500
  const shouldSkipValidation = (!hasValidation(field._f) &&
1501
+ !props.validate &&
1435
1502
  !_options.resolver &&
1436
1503
  !get(_formState.errors, name) &&
1437
1504
  !field._f.deps) ||
@@ -1469,6 +1536,12 @@ function createFormControl(props = {}) {
1469
1536
  return (shouldRender &&
1470
1537
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1471
1538
  }
1539
+ if (!_options.resolver && props.validate) {
1540
+ await validateForm({
1541
+ name: name,
1542
+ eventType: event.type,
1543
+ });
1544
+ }
1472
1545
  !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1473
1546
  if (_options.resolver) {
1474
1547
  const { errors } = await _runSchema([name]);
@@ -1498,7 +1571,12 @@ function createFormControl(props = {}) {
1498
1571
  }
1499
1572
  else if (_proxyFormState.isValid ||
1500
1573
  _proxySubscribeFormState.isValid) {
1501
- isValid = await executeBuiltInValidation(_fields, true);
1574
+ isValid = await executeBuiltInValidation({
1575
+ fields: _fields,
1576
+ onlyCheckValid: true,
1577
+ name: name,
1578
+ eventType: event.type,
1579
+ });
1502
1580
  }
1503
1581
  }
1504
1582
  }
@@ -1531,12 +1609,19 @@ function createFormControl(props = {}) {
1531
1609
  else if (name) {
1532
1610
  validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
1533
1611
  const field = get(_fields, fieldName);
1534
- return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
1612
+ return await executeBuiltInValidation({
1613
+ fields: field && field._f ? { [fieldName]: field } : field,
1614
+ eventType: EVENTS.TRIGGER,
1615
+ });
1535
1616
  }))).every(Boolean);
1536
1617
  !(!validationResult && !_formState.isValid) && _setValid();
1537
1618
  }
1538
1619
  else {
1539
- validationResult = isValid = await executeBuiltInValidation(_fields);
1620
+ validationResult = isValid = await executeBuiltInValidation({
1621
+ fields: _fields,
1622
+ name,
1623
+ eventType: EVENTS.TRIGGER,
1624
+ });
1540
1625
  }
1541
1626
  _subjects.state.next({
1542
1627
  ...(!isString(name) ||
@@ -1843,14 +1928,17 @@ function createFormControl(props = {}) {
1843
1928
  fieldValues = cloneObject(values);
1844
1929
  }
1845
1930
  else {
1846
- await executeBuiltInValidation(_fields);
1931
+ await executeBuiltInValidation({
1932
+ fields: _fields,
1933
+ eventType: EVENTS.SUBMIT,
1934
+ });
1847
1935
  }
1848
1936
  if (_names.disabled.size) {
1849
1937
  for (const name of _names.disabled) {
1850
1938
  unset(fieldValues, name);
1851
1939
  }
1852
1940
  }
1853
- unset(_formState.errors, 'root');
1941
+ unset(_formState.errors, ROOT_ERROR_TYPE);
1854
1942
  if (isEmptyObject(_formState.errors)) {
1855
1943
  _subjects.state.next({
1856
1944
  errors: {},