the validation of email address does not work using DDD pattern, i am using dartz, bloc and freezed to do so. i have tried to discover the error but i can Not.
the email address is:
class _EmailAdressInput extends _ListOfInputs { const _EmailAdressInput(); @override Widget build(BuildContext context) { return BlocBuilder<AuthBloc, AuthBlocState>(builder: (context, state) { return TextFormField( focusNode: emailAddressNode, autocorrect: false, textInputAction: TextInputAction.next, decoration: InputDecoration( labelText: 'Email Address', ), validator: (value) { return context.read<AuthBloc>().state.email.value!.fold( (l) => l.maybeMap( orElse: () {}, invalidEmail: (_) => "Invalid Email"), (r) => null); }, onChanged: (value) { return context.read<AuthBloc>().add( AuthBlocEvent.emailAddressEvent(emailAdressEventValue: value)); }, onFieldSubmitted: (value) { emailAddressNode!.unfocus(); FocusScope.of(context).requestFocus(passwordNode); }, ); }); } }
the event using freezed:
@freezed abstract class AuthBlocEvent with _$AuthBlocEvent { const factory AuthBlocEvent.emailAddressEvent( {required String emailAdressEventValue}) = _EmailAddressEvent; }
the state using freezed library is:
@freezed abstract class AuthBlocState with _$AuthBlocState { const factory AuthBlocState({ required Password password, required bool isSubmitted, required bool showErrorMessages, required Option<Either<AuthFailures, Unit>> authFailureOrSuccessOption, }) = _AuthBlocState; factory AuthBlocState.initial() => AuthBlocState( email: EmailAddress(''), isSubmitted: false, showErrorMessages: false, authFailureOrSuccessOption: none(), ); }
the bloc is:
@override Stream<AuthBlocState> mapEventToState( AuthBlocEvent event, ) async* { yield* event.map( emailAddressEvent: (e) async* { yield state.copyWith( email: EmailAddress(e.emailAdressEventValue), showErrorMessages: true, authFailureOrSuccessOption: none(), ); }, okPressedEvent: (e) async* { // to get the value of // either we should use option of // to check either values // we have to knew that // both are right if (state.email.isValueValid() ) { yield state.copyWith( isSubmitted: true, authFailureOrSuccessOption: none(), ); } }, );}
the validator is:
Either<ValueFailures, String> emailAddressValidator(String input) { if (RegExp(r"""^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+""") .hasMatch(input)) return right(input); return left(const ValueFailures.invalidEmail( msg: "Invalid Email")); }
the is value object for equality is:
abstract class ValueObject { const ValueObject(); Either<ValueFailures, String>? get value; Either<ValueFailures, String>? get extraValue; bool isValueValid() => value!.isRight(); bool isExtraValueValid() => extraValue!.isRight(); @override String toString() => 'ValueObject(value: $value, extraValue: $extraValue)'; @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is ValueObject && other.value == value && other.extraValue == extraValue; } @override int get hashCode => value.hashCode ^ extraValue.hashCode; }
https://stackoverflow.com/questions/66956994/text-validator-does-not-work-using-bloc-and-freezed April 06, 2021 at 01:19AM
没有评论:
发表评论