Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can{t get model with a FormGroup inside a FormArray inside a nested FormGroup #154

Open
fershous opened this issue Mar 16, 2024 · 2 comments

Comments

@fershous
Copy link

Here are the models:

/// [UserProfile] model
@freezed
@ReactiveFormAnnotation()
@FormGroupAnnotation()
class UserProfile with _$UserProfile, BaseUser, NexusElement {
  const factory UserProfile({
    required String id,
    @RfControl(validators: [RequiredValidator()]) required String name,
    required UserProfileType type,
  }) = _UserProfile;

  /// Serialization
  factory UserProfile.fromJson(Map<String, dynamic> json) => _$UserProfileFromJson(json);
}

@ReactiveFormAnnotation()
@FormGroupAnnotation()
class UserProfileType {
  UserProfileType({
    @FormArrayAnnotation() required this.permissions,
    @FormControlAnnotation() required this.value,
  });

  final List<PermissionEntity> permissions;
  final int value;
}

@ReactiveFormAnnotation()
@FormGroupAnnotation()
class PermissionEntity {
  PermissionEntity({
    @FormControlAnnotation() required this.permission,
    @FormControlAnnotation() required this.value,
  });

  final ProfilePermissionsEnum permission;
  final bool value;
}

The problem is that the PermissionEntity is being created as a FormControl<Map<String, Object?>> and it won't read it as a FormGroup . Unless I'm doing something wrong, I think there's something bad happening.

I have used FormArrays as FormGroups and indeed it read it as FormControl<Map<String, Object?>> but it works if I just cast it as FormGroup maybe a cast is missing when getting the value.

@fershous
Copy link
Author

So, I simple it down to this:

part 'user_profile.freezed.dart';

part 'user_profile.g.dart';

part 'user_profile.gform.dart';

/// [UserProfile] model
@freezed
@ReactiveFormAnnotation()
class UserProfile with _$UserProfile, BaseUser, NexusElement, RestrictedPermissions {
  const UserProfile._();

  /// Factory constructor
  /// [id] - [UserProfile] id
  @JsonSerializable(fieldRename: FieldRename.snake, explicitToJson: true)
  const factory UserProfile({
    required String id,
    //
    @FormControlAnnotation(validators: [RequiredValidator()]) required String name,
    //
    @JsonKey(name: "pass")
    @FormControlAnnotation(validators: [RequiredValidator()])
    required String passwordHash,
    //
    @JsonKey(
      name: 'perm',
      fromJson: _userProfilePermissionsFromJson,
      toJson: _userProfilePermissionsToJson,
    )
    @FormArrayAnnotation()
    required List<PermissionEntity> permissions,
    //
    @JsonKey(name: "bdte", fromJson: fromJsonTimestamp, toJson: toJsonTimestamp)
    @FormControlAnnotation()
    required DateTime? birthDate,
    //
    @JsonKey(name: "tel") @FormControlAnnotation() required int? phoneNumber,
  }) = _UserProfile;

  /// Serialization
  factory UserProfile.fromJson(Map<String, dynamic> json) => _$UserProfileFromJson(json);

  bool validate(String enteredPassword) {
    return Crypt(passwordHash).match(enteredPassword);
  }
}

@FormGroupAnnotation()
class PermissionEntity {
  PermissionEntity({
    @FormControlAnnotation() required this.permission,
    @FormControlAnnotation() required this.value,
  });

  final ProfilePermissionsEnum permission;
  final bool value;
}

Now if I try to print the model it works. But I can't build the ReactiveFormArrayBuilder here's the code

ReactiveFormArray<Map<String, Object?>>(
  formArray: formModel.permissionsControl,
  builder: (context, formArray, child) {
    return Column(
    children: formModel.permissionsControl.controls
      .asMap()
      .map((i, deliveryPoint) {
        return MapEntry(
          i,
          Column(
            children: [
              ReactiveCheckbox(
                formControlName: '${i}.value',
              ),
             ],
           ),
        );
        })
       .values
       .toList(),
    );
  },
),

The problem is that it won't find the {i}.value as there's no way to find the control because it builds the array as a normal array and not putting controls in it.

Am I doing something worng?

I'm using:

reactive_forms_generator: ^4.5.0
reactive_forms: ^16.1.1
reactive_forms_annotations: ^4.3.0

@qwezey
Copy link

qwezey commented Sep 23, 2024

im facing the same issue. @fershous have you found any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants