Merge pull request #4 from DieLustigenTierwesen/copilot/sub-pr-1-another-one
Add input validation for collection names
This commit is contained in:
commit
69b54de89d
2 changed files with 63 additions and 37 deletions
|
|
@ -50,6 +50,7 @@ class CollectionsScreenState extends State<CollectionsScreen> {
|
|||
}
|
||||
|
||||
Future<void> _createCollection() async {
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final nameCtrl = TextEditingController();
|
||||
final descCtrl = TextEditingController();
|
||||
|
||||
|
|
@ -65,16 +66,26 @@ class CollectionsScreenState extends State<CollectionsScreen> {
|
|||
child: const Icon(Icons.add, color: Colors.white, size: 28),
|
||||
),
|
||||
title: const Text('New Collection'),
|
||||
content: Column(
|
||||
content: Form(
|
||||
key: formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
TextFormField(
|
||||
controller: nameCtrl,
|
||||
autofocus: true,
|
||||
maxLength: 50,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Name',
|
||||
hintText: 'e.g. Hot Wheels, Matchbox…',
|
||||
),
|
||||
validator: (value) {
|
||||
final trimmed = value?.trim() ?? '';
|
||||
if (trimmed.isEmpty) return 'Name is required';
|
||||
if (trimmed.length < 2) return 'Name must be at least 2 characters';
|
||||
if (trimmed.length > 50) return 'Name must be 50 characters or fewer';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
|
|
@ -86,6 +97,7 @@ class CollectionsScreenState extends State<CollectionsScreen> {
|
|||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
|
|
@ -93,8 +105,9 @@ class CollectionsScreenState extends State<CollectionsScreen> {
|
|||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
if (nameCtrl.text.trim().isEmpty) return;
|
||||
if (formKey.currentState!.validate()) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
},
|
||||
child: const Text('Create'),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ class _ManageCollectionScreenState extends State<ManageCollectionScreen> {
|
|||
}
|
||||
|
||||
Future<void> _rename() async {
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final ctrl = TextEditingController(text: _collection.name);
|
||||
final descCtrl =
|
||||
TextEditingController(text: _collection.description ?? '');
|
||||
|
|
@ -52,13 +53,23 @@ class _ManageCollectionScreenState extends State<ManageCollectionScreen> {
|
|||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Rename Collection'),
|
||||
content: Column(
|
||||
content: Form(
|
||||
key: formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
TextFormField(
|
||||
controller: ctrl,
|
||||
autofocus: true,
|
||||
maxLength: 50,
|
||||
decoration: const InputDecoration(labelText: 'Name'),
|
||||
validator: (value) {
|
||||
final trimmed = value?.trim() ?? '';
|
||||
if (trimmed.isEmpty) return 'Name is required';
|
||||
if (trimmed.length < 2) return 'Name must be at least 2 characters';
|
||||
if (trimmed.length > 50) return 'Name must be 50 characters or fewer';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
|
|
@ -68,6 +79,7 @@ class _ManageCollectionScreenState extends State<ManageCollectionScreen> {
|
|||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
|
|
@ -75,8 +87,9 @@ class _ManageCollectionScreenState extends State<ManageCollectionScreen> {
|
|||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
if (ctrl.text.trim().isEmpty) return;
|
||||
if (formKey.currentState!.validate()) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
},
|
||||
child: const Text('Save'),
|
||||
),
|
||||
|
|
|
|||
Loading…
Reference in a new issue