NyFormData constructor

NyFormData(
  1. String name
)

Implementation

NyFormData(String this.name) {
  dynamic formFields = fields();

  Map<String, dynamic> allData = {};
  for (dynamic formField in formFields) {
    if (formField is List) {
      for (Field field in formField) {
        allData.addAll(fieldData(field));
        _cast[field.key] = field.cast;
        _validate[field.key] = field.validate;
        _dummyData[field.key] = field.dummyData;
        _header[field.key] = field.header;
        _footer[field.key] = field.footer;
        _style[field.key] = field.style;
        _metaData[field.key] = field.metaData;
        _hiddenData[field.key] = field.hidden;
        if (field.autofocus == true) {
          if (_getAutoFocusedField != null) {
            throw Exception(
                "Only one field can be set to autofocus in a form");
          }
          _getAutoFocusedField = field.key;
        }
        _keys.add(field.key);
      }
      _groupedItems.add([for (Field field in formField) field.key]);
      continue;
    }

    if (formField is! Field) {
      continue;
    }

    allData.addAll(fieldData(formField));
    _groupedItems.add([formField.key]);

    Map<String, dynamic> formCast = _cast;
    Map<String, dynamic> formValidate = _validate;
    Map<String, dynamic> formDummyData = _dummyData;
    Map<String, dynamic> formStyle = _style;

    _cast[formField.key] = formField.cast;

    formCast.entries
        .where((test) => test.key == formField.key)
        .forEach((cast) {
      _cast[formField.key] = cast.value;
    });

    // Check if the field has a validate
    if (formField.validate != null) {
      _validate[formField.key] = formField.validate;
    }
    formValidate.entries
        .where((test) => test.key == formField.key)
        .forEach((validate) {
      _validate[formField.key] = validate.value;
    });

    if (!_validate.containsKey(formField.key)) {
      _validate[formField.key] = null;
    }

    if (formField.header != null) {
      _header[formField.key] = formField.header;
    }

    if (!_header.containsKey(formField.key)) {
      _header[formField.key] = null;
    }

    if (formField.footer != null) {
      _footer[formField.key] = formField.footer;
    }

    if (!_footer.containsKey(formField.key)) {
      _footer[formField.key] = null;
    }

    if (formField.hidden != null) {
      _hiddenData[formField.key] = formField.hidden;
    }

    if (!_hiddenData.containsKey(formField.key)) {
      _hiddenData[formField.key] = null;
    }

    if (formField.metaData != null) {
      _metaData[formField.key] = formField.metaData;
    }

    if (!_metaData.containsKey(formField.key)) {
      _metaData[formField.key] = null;
    }

    // Check if the field has a dummy data
    if (formField.dummyData != null) {
      _dummyData[formField.key] = formField.dummyData;
    }
    formDummyData.entries
        .where((test) => test.key == formField.key)
        .forEach((dummyData) {
      _dummyData[formField.key] = dummyData.value;
    });

    if (!_dummyData.containsKey(formField.key)) {
      _dummyData[formField.key] = null;
    }

    // Check if the field has a style
    if (formField.style != null) {
      _style[formField.key] = formField.style;
    }
    formStyle.entries
        .where((test) => test.key == formField.key)
        .forEach((style) {
      _style[formField.key] = style.value;
    });

    if (!_style.containsKey(formField.key)) {
      _style[formField.key] = null;
    }

    if (formField.autofocus == true) {
      if (_getAutoFocusedField != null) {
        throw Exception("Only one field can be set to autofocus in a form");
      }
      _getAutoFocusedField = formField.key;
    }

    _keys.add(formField.key);
  }

  setData(allData, refreshState: false);
  if (getEnv('APP_ENV') != 'developing') {
    return;
  }
  Map<String, dynamic> allDummyData = getDummyData;
  for (var data in allDummyData.entries) {
    if (data.value != null) {
      setFieldValue(data.key, data.value, refreshState: false);
    }
  }
}