Merge pull request #1 from traccar/codex/validate-and-show-error-for-server-url

Add URL validation
This commit is contained in:
Anton Tananaev 2025-06-01 12:45:43 -07:00 committed by GitHub
commit b143d8c354
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View file

@ -8,6 +8,7 @@
"locationButton": "Send location",
"statusButton": "Show status",
"settingsButton": "Change settings",
"invalidValue": "Invalid value",
"idLabel": "Device identifier",
"urlLabel": "Server URL",
"accuracyLabel": "Location accuracy",

View file

@ -43,6 +43,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
: Preferences.instance.getString(key) ?? '';
final controller = TextEditingController(text: initialValue);
final scaffoldManager = ScaffoldMessenger.of(context);
final errorMessage = AppLocalizations.of(context)!.invalidValue;
final result = await showDialog<String>(
context: context,
@ -67,6 +69,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
if (result != null && result.isNotEmpty) {
if (key == Preferences.url) {
final uri = Uri.tryParse(result);
if (uri == null || uri.host.isEmpty || !(uri.scheme == 'http' || uri.scheme == 'https')) {
scaffoldManager.showSnackBar(SnackBar(content: Text(errorMessage)));
return;
}
}
if (isInt) {
final intValue = int.tryParse(result);
if (intValue != null) {