artflutter / reactive_forms_generator

Other
81 stars 22 forks source link

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

Open fershous opened 3 months ago

fershous commented 3 months ago

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 commented 3 months ago

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