add username validation
This commit is contained in:
@@ -33,16 +33,35 @@ class _MyTextFieldState extends State<MyTextField> {
|
|||||||
|
|
||||||
String? get _errorText {
|
String? get _errorText {
|
||||||
final text = widget.controller.text;
|
final text = widget.controller.text;
|
||||||
|
String _errorMessage = '';
|
||||||
if (startup) {
|
if (startup) {
|
||||||
return null;
|
return null;
|
||||||
} else if (!widget.required) {
|
|
||||||
return null;
|
|
||||||
} else if (text.isEmpty) {
|
|
||||||
return "${widget.hintText} is required";
|
|
||||||
} else if (widget.hintText == "Email" && !isEmailValid(text)) {
|
|
||||||
return "Enter a valid email address";
|
|
||||||
}
|
}
|
||||||
return null;
|
if (!widget.required) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (text.isEmpty) {
|
||||||
|
return "${widget.hintText} is required";
|
||||||
|
}
|
||||||
|
if (widget.hintText == "Email" && !isEmailValid(text)) {
|
||||||
|
_errorMessage += "Enter a valid email address\n";
|
||||||
|
}
|
||||||
|
if (widget.hintText == "Username" && text.length < 8) {
|
||||||
|
_errorMessage += "• Username must contain at least 8 characters.\n";
|
||||||
|
}
|
||||||
|
if (widget.hintText == "Username" && !isUsernameValid(text)) {
|
||||||
|
_errorMessage += "• Username can only contain '_' special Chracters.\n";
|
||||||
|
}
|
||||||
|
if (_errorMessage.isEmpty) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// If there are no error messages, the password is valid
|
||||||
|
return _errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isUsernameValid(String Username) {
|
||||||
|
return RegExp(r'^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$')
|
||||||
|
.hasMatch(Username);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isEmailValid(String email) {
|
bool isEmailValid(String email) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:patient_manager/components/myErrorMessage.dart';
|
import 'package:patient_manager/components/myErrorMessage.dart';
|
||||||
import 'package:patient_manager/components/mySuccessMessage.dart';
|
import 'package:patient_manager/components/mySuccessMessage.dart';
|
||||||
import 'package:patient_manager/components/myTextInput.dart';
|
import 'package:patient_manager/components/myTextInput.dart';
|
||||||
@@ -27,6 +28,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
final fnameController = TextEditingController();
|
final fnameController = TextEditingController();
|
||||||
final lnameController = TextEditingController();
|
final lnameController = TextEditingController();
|
||||||
late bool businessUser;
|
late bool businessUser;
|
||||||
|
final FocusNode _focusNode = FocusNode();
|
||||||
|
|
||||||
bool isFieldsFilled() {
|
bool isFieldsFilled() {
|
||||||
if (fnameController.text.isEmpty ||
|
if (fnameController.text.isEmpty ||
|
||||||
@@ -52,30 +54,34 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
} else {
|
} else {
|
||||||
profileType = "personal";
|
profileType = "personal";
|
||||||
}
|
}
|
||||||
|
print("is username valid ${isUsernameValid(usernameController.text)}");
|
||||||
var response = await http.put(
|
if (isUsernameValid(usernameController.text) == false) {
|
||||||
Uri.parse("${AppEnviroment.baseApiUrl}/user/update/"),
|
usernamePopUp();
|
||||||
headers: <String, String>{
|
|
||||||
"Content-Type": "application/json; charset=UTF-8"
|
|
||||||
},
|
|
||||||
body: jsonEncode(<String, dynamic>{
|
|
||||||
"idusers": widget.signedInUser.idUser,
|
|
||||||
"username": usernameController.text,
|
|
||||||
"fnam": fnameController.text,
|
|
||||||
"lname": lnameController.text,
|
|
||||||
"type": profileType,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
//print("Here4");
|
|
||||||
//print(response.statusCode);
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
Navigator.of(context)
|
|
||||||
.popAndPushNamed('/', arguments: widget.signedInUser);
|
|
||||||
String message =
|
|
||||||
"${widget.signedInUser.email}'s information has been updated successfully!";
|
|
||||||
successPopUp(message);
|
|
||||||
} else {
|
} else {
|
||||||
internetConnectionPopUp();
|
var response = await http.put(
|
||||||
|
Uri.parse("${AppEnviroment.baseApiUrl}/user/update/"),
|
||||||
|
headers: <String, String>{
|
||||||
|
"Content-Type": "application/json; charset=UTF-8"
|
||||||
|
},
|
||||||
|
body: jsonEncode(<String, dynamic>{
|
||||||
|
"idusers": widget.signedInUser.idUser,
|
||||||
|
"username": usernameController.text,
|
||||||
|
"fnam": fnameController.text,
|
||||||
|
"lname": lnameController.text,
|
||||||
|
"type": profileType,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
//print("Here4");
|
||||||
|
//print(response.statusCode);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
Navigator.of(context)
|
||||||
|
.popAndPushNamed('/', arguments: widget.signedInUser);
|
||||||
|
String message =
|
||||||
|
"${widget.signedInUser.email}'s information has been updated successfully!";
|
||||||
|
successPopUp(message);
|
||||||
|
} else {
|
||||||
|
internetConnectionPopUp();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,6 +114,33 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void usernamePopUp() {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return const MyErrorMessage(errorType: "Invalid Username");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isUsernameValid(String username) {
|
||||||
|
return RegExp(r'^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$')
|
||||||
|
.hasMatch(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
void submitForm() {
|
||||||
|
if (isFieldsFilled()) {
|
||||||
|
updateUserApiCall();
|
||||||
|
} else {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return const MyErrorMessage(errorType: "Input Error");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -122,85 +155,86 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Column(
|
return KeyboardListener(
|
||||||
children: [
|
focusNode: _focusNode,
|
||||||
const Text(
|
autofocus: true,
|
||||||
"Personal profile:",
|
onKeyEvent: (event) async {
|
||||||
style: TextStyle(
|
if (event is KeyDownEvent &&
|
||||||
fontWeight: FontWeight.bold,
|
event.logicalKey == LogicalKeyboardKey.enter) {
|
||||||
fontSize: 25,
|
submitForm();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
"Personal profile:",
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 25,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(height: 15.0),
|
||||||
const SizedBox(height: 15.0),
|
MyTextField(
|
||||||
MyTextField(
|
controller: usernameController,
|
||||||
controller: usernameController,
|
hintText: "Username",
|
||||||
hintText: "Username",
|
editable: true,
|
||||||
editable: true,
|
required: true,
|
||||||
required: true,
|
),
|
||||||
),
|
const SizedBox(height: 10.0),
|
||||||
const SizedBox(height: 10.0),
|
MyTextField(
|
||||||
MyTextField(
|
controller: fnameController,
|
||||||
controller: fnameController,
|
hintText: "First Name",
|
||||||
hintText: "First Name",
|
editable: true,
|
||||||
editable: true,
|
required: true,
|
||||||
required: true,
|
),
|
||||||
),
|
const SizedBox(height: 10.0),
|
||||||
const SizedBox(height: 10.0),
|
MyTextField(
|
||||||
MyTextField(
|
controller: lnameController,
|
||||||
controller: lnameController,
|
hintText: "Last Name",
|
||||||
hintText: "Last Name",
|
editable: true,
|
||||||
editable: true,
|
required: true,
|
||||||
required: true,
|
),
|
||||||
),
|
const SizedBox(height: 10.0),
|
||||||
const SizedBox(height: 10.0),
|
Row(
|
||||||
Row(
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
children: [
|
||||||
children: [
|
const Text(
|
||||||
const Text(
|
"Activate Business Account",
|
||||||
"Activate Business",
|
style: TextStyle(
|
||||||
style: TextStyle(
|
fontWeight: FontWeight.bold,
|
||||||
fontWeight: FontWeight.bold,
|
fontSize: 20,
|
||||||
fontSize: 20,
|
),
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(
|
||||||
const SizedBox(
|
width: 25,
|
||||||
width: 25,
|
),
|
||||||
),
|
Switch(
|
||||||
Switch(
|
value: businessUser,
|
||||||
value: businessUser,
|
onChanged: (bool value) {
|
||||||
onChanged: (bool value) {
|
setState(() {
|
||||||
setState(() {
|
businessUser = value;
|
||||||
businessUser = value;
|
});
|
||||||
});
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 10.0),
|
||||||
|
SizedBox(
|
||||||
|
width: 500.0,
|
||||||
|
height: 100.0,
|
||||||
|
child: MyButton(
|
||||||
|
buttonText: "Update",
|
||||||
|
buttonColor:
|
||||||
|
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
|
||||||
|
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
|
||||||
|
onTap: () {
|
||||||
|
submitForm();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
|
||||||
),
|
|
||||||
const SizedBox(height: 10.0),
|
|
||||||
SizedBox(
|
|
||||||
width: 500.0,
|
|
||||||
height: 100.0,
|
|
||||||
child: MyButton(
|
|
||||||
buttonText: "Update",
|
|
||||||
buttonColor:
|
|
||||||
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
|
|
||||||
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
|
|
||||||
onTap: () {
|
|
||||||
if (isFieldsFilled()) {
|
|
||||||
updateUserApiCall();
|
|
||||||
} else {
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) {
|
|
||||||
return const MyErrorMessage(errorType: "Input Error");
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user