reinhardt-forms 0.1.0-rc.15

Form handling and validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! ModelForm implementation for ORM integration
//!
//! ModelForms automatically generate forms from ORM models, handling field
//! inference, validation, and saving.

use crate::{CharField, EmailField, FloatField, Form, FormError, FormField, IntegerField, Widget};
use serde_json::Value;
use std::collections::HashMap;
use std::marker::PhantomData;

/// Field type metadata for ModelForm field inference
#[derive(Debug, Clone)]
pub enum FieldType {
	/// Character field with optional maximum length constraint.
	Char {
		/// Maximum character length, if any.
		max_length: Option<usize>,
	},
	/// Multi-line text field rendered as a textarea.
	Text,
	/// Integer number field.
	Integer,
	/// Floating-point number field.
	Float,
	/// Boolean field rendered as a checkbox.
	Boolean,
	/// Date and time field.
	DateTime,
	/// Date-only field.
	Date,
	/// Time-only field.
	Time,
	/// Email address field with format validation.
	Email,
	/// URL field with format validation.
	Url,
	/// JSON data field.
	Json,
}

/// Trait for models that can be used with ModelForm
///
/// This trait is specifically for form models. For ORM models, use `reinhardt_db::orm::Model`.
pub trait FormModel: Send + Sync {
	/// Get the model's field names
	fn field_names() -> Vec<String>;

	/// Get field type metadata for form field inference
	///
	/// # Examples
	///
	/// ```no_run
	/// # use reinhardt_forms::model_form::FieldType;
	/// fn field_type(name: &str) -> Option<FieldType> {
	///     match name {
	///         "name" => Some(FieldType::Char { max_length: Some(100) }),
	///         "email" => Some(FieldType::Email),
	///         "age" => Some(FieldType::Integer),
	///         _ => None,
	///     }
	/// }
	/// ```
	fn field_type(_name: &str) -> Option<FieldType> {
		None
	}

	/// Get a field value by name
	fn get_field(&self, name: &str) -> Option<Value>;

	/// Set a field value by name
	fn set_field(&mut self, name: &str, value: Value) -> Result<(), String>;

	/// Save the model to the database
	fn save(&mut self) -> Result<(), String>;

	/// Validate the model
	fn validate(&self) -> Result<(), Vec<String>> {
		Ok(())
	}

	/// Convert model instance to a choice label for display in forms
	///
	/// Default implementation returns the string representation of the primary key.
	/// Override this method to provide custom display labels.
	///
	/// # Examples
	///
	/// ```no_run
	/// # struct Example { id: i32, name: String }
	/// # impl Example {
	/// fn to_choice_label(&self) -> String {
	///     format!("{} - {}", self.id, self.name)
	/// }
	/// # }
	/// ```
	fn to_choice_label(&self) -> String {
		// Default: use the "id" field or empty string
		self.get_field("id")
			.and_then(|v| v.as_i64().map(|i| i.to_string()))
			.or_else(|| {
				self.get_field("id")
					.and_then(|v| v.as_str().map(|s| s.to_string()))
			})
			.unwrap_or_default()
	}

	/// Get the primary key value as a string for form field validation
	///
	/// Default implementation uses the "id" field.
	///
	/// # Examples
	///
	/// ```no_run
	/// # struct Example { id: i32 }
	/// # impl Example {
	/// fn to_choice_value(&self) -> String {
	///     self.id.to_string()
	/// }
	/// # }
	/// ```
	fn to_choice_value(&self) -> String {
		self.get_field("id")
			.and_then(|v| v.as_i64().map(|i| i.to_string()))
			.or_else(|| {
				self.get_field("id")
					.and_then(|v| v.as_str().map(|s| s.to_string()))
			})
			.unwrap_or_default()
	}
}

/// ModelForm configuration
#[derive(Debug, Clone, Default)]
pub struct ModelFormConfig {
	/// Fields to include in the form (None = all fields)
	pub fields: Option<Vec<String>>,
	/// Fields to exclude from the form
	pub exclude: Vec<String>,
	/// Custom widgets for specific fields
	pub widgets: HashMap<String, crate::Widget>,
	/// Custom labels for specific fields
	pub labels: HashMap<String, String>,
	/// Custom help text for specific fields
	pub help_texts: HashMap<String, String>,
}

impl ModelFormConfig {
	/// Creates a new default configuration.
	pub fn new() -> Self {
		Self::default()
	}
	/// Sets the list of fields to include in the generated form.
	pub fn fields(mut self, fields: Vec<String>) -> Self {
		self.fields = Some(fields);
		self
	}
	/// Sets the list of fields to exclude from the generated form.
	pub fn exclude(mut self, exclude: Vec<String>) -> Self {
		self.exclude = exclude;
		self
	}
	/// Overrides the widget for a specific field.
	pub fn widget(mut self, field: String, widget: crate::Widget) -> Self {
		self.widgets.insert(field, widget);
		self
	}
	/// Sets a custom label for a specific field.
	pub fn label(mut self, field: String, label: String) -> Self {
		self.labels.insert(field, label);
		self
	}
	/// Sets custom help text for a specific field.
	pub fn help_text(mut self, field: String, text: String) -> Self {
		self.help_texts.insert(field, text);
		self
	}
}

/// A form that is automatically generated from a Model
pub struct ModelForm<T: FormModel> {
	form: Form,
	instance: Option<T>,
	// Allow dead_code: config stored for future form rendering customization
	#[allow(dead_code)]
	config: ModelFormConfig,
	_phantom: PhantomData<T>,
}

impl<T: FormModel> ModelForm<T> {
	/// Create a form field from field type metadata
	fn create_form_field(
		name: &str,
		field_type: FieldType,
		config: &ModelFormConfig,
	) -> Box<dyn FormField> {
		let label = config.labels.get(name).cloned();
		let help_text = config.help_texts.get(name).cloned();
		let widget = config.widgets.get(name).cloned();

		match field_type {
			FieldType::Char { max_length } => {
				let mut field = CharField::new(name.to_string());
				if let Some(label) = label {
					field.label = Some(label);
				}
				if let Some(help) = help_text {
					field.help_text = Some(help);
				}
				if let Some(w) = widget {
					field.widget = w;
				}
				field.max_length = max_length;
				Box::new(field)
			}
			FieldType::Text => {
				let mut field = CharField::new(name.to_string());
				if let Some(label) = label {
					field.label = Some(label);
				}
				if let Some(help) = help_text {
					field.help_text = Some(help);
				}
				if let Some(w) = widget {
					field.widget = w;
				} else {
					field.widget = Widget::TextArea;
				}
				Box::new(field)
			}
			FieldType::Email => {
				let mut field = EmailField::new(name.to_string());
				if let Some(label) = label {
					field.label = Some(label);
				}
				if let Some(help) = help_text {
					field.help_text = Some(help);
				}
				if let Some(w) = widget {
					field.widget = w;
				}
				Box::new(field)
			}
			FieldType::Integer => {
				let mut field = IntegerField::new(name.to_string());
				if let Some(label) = label {
					field.label = Some(label);
				}
				if let Some(help) = help_text {
					field.help_text = Some(help);
				}
				if let Some(w) = widget {
					field.widget = w;
				}
				Box::new(field)
			}
			FieldType::Float => {
				let mut field = FloatField::new(name.to_string());
				if let Some(label) = label {
					field.label = Some(label);
				}
				if let Some(help) = help_text {
					field.help_text = Some(help);
				}
				if let Some(w) = widget {
					field.widget = w;
				}
				Box::new(field)
			}
			// For unsupported types, default to CharField
			_ => {
				let mut field = CharField::new(name.to_string());
				if let Some(label) = label {
					field.label = Some(label);
				}
				if let Some(help) = help_text {
					field.help_text = Some(help);
				}
				if let Some(w) = widget {
					field.widget = w;
				}
				Box::new(field)
			}
		}
	}

	/// Create a new ModelForm from a model instance
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_forms::{ModelForm, ModelFormConfig};
	///
	/// // Assuming we have a model that implements the Model trait
	/// let config = ModelFormConfig::new();
	/// let form = ModelForm::new(Some(instance), config);
	/// ```
	pub fn new(instance: Option<T>, config: ModelFormConfig) -> Self {
		let mut form = Form::new();

		// Get field names from model
		let all_fields = T::field_names();

		// Filter fields based on config
		let fields_to_include: Vec<String> = if let Some(ref include) = config.fields {
			include
				.iter()
				.filter(|f| !config.exclude.contains(f))
				.cloned()
				.collect()
		} else {
			all_fields
				.iter()
				.filter(|f| !config.exclude.contains(f))
				.cloned()
				.collect()
		};

		// Infer field types from model metadata and add to form
		for field_name in &fields_to_include {
			if let Some(field_type) = T::field_type(field_name) {
				let form_field = Self::create_form_field(field_name, field_type, &config);
				form.add_field(form_field);
			}
		}

		// If instance exists, populate initial data from the instance
		if let Some(ref inst) = instance {
			let mut initial = HashMap::new();
			for field_name in &fields_to_include {
				if let Some(value) = inst.get_field(field_name) {
					initial.insert(field_name.clone(), value);
				}
			}
			form.bind(initial);
		}

		Self {
			form,
			instance,
			config,
			_phantom: PhantomData,
		}
	}
	/// Create a new ModelForm without an instance (for creation)
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_forms::{ModelForm, ModelFormConfig};
	///
	/// let config = ModelFormConfig::new();
	/// let form = ModelForm::<MyModel>::empty(config);
	/// ```
	pub fn empty(config: ModelFormConfig) -> Self {
		Self::new(None, config)
	}
	/// Bind data to the form
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_forms::{ModelForm, ModelFormConfig};
	/// use std::collections::HashMap;
	/// use serde_json::json;
	///
	/// let config = ModelFormConfig::new();
	/// let mut form = ModelForm::<MyModel>::empty(config);
	/// let mut data = HashMap::new();
	/// data.insert("field".to_string(), json!("value"));
	/// form.bind(data);
	/// ```
	pub fn bind(&mut self, data: HashMap<String, Value>) -> &mut Self {
		// Bind data to the underlying form
		self.form.bind(data);
		self
	}
	/// Check if the form is valid
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_forms::{ModelForm, ModelFormConfig};
	///
	/// let config = ModelFormConfig::new();
	/// let mut form = ModelForm::<MyModel>::empty(config);
	/// let is_valid = form.is_valid();
	/// ```
	pub fn is_valid(&mut self) -> bool {
		// Validate the model if instance exists
		if let Some(ref instance) = self.instance
			&& let Err(_errors) = instance.validate()
		{
			return false;
		}

		true
	}
	/// Save the form data to the model instance
	///
	/// Returns `FormError::NoInstance` if no model instance is available.
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_forms::{ModelForm, ModelFormConfig};
	///
	/// let config = ModelFormConfig::new();
	/// let mut form = ModelForm::<MyModel>::empty(config);
	/// // Returns Err(FormError::NoInstance) without an instance
	/// assert!(form.save().is_err());
	/// ```
	pub fn save(&mut self) -> Result<T, FormError> {
		if !self.is_valid() {
			return Err(FormError::Validation("Form is not valid".to_string()));
		}

		// Get existing instance or return error
		let mut instance = self.instance.take().ok_or(FormError::NoInstance)?;

		// Set field values from form's cleaned_data
		let cleaned_data = self.form.cleaned_data();
		for (field_name, value) in cleaned_data.iter() {
			if let Err(e) = instance.set_field(field_name, value.clone()) {
				return Err(FormError::Validation(format!(
					"Failed to set field {}: {}",
					field_name, e
				)));
			}
		}

		// Save the instance
		if let Err(e) = instance.save() {
			return Err(FormError::Validation(format!("Failed to save: {}", e)));
		}

		Ok(instance)
	}
	/// Set a field value directly on the model instance.
	///
	/// This is used by `InlineFormSet` to set foreign key values on child
	/// instances before saving.
	///
	/// If no instance exists, this method is a no-op.
	pub fn set_field_value(&mut self, field_name: &str, value: Value) {
		if let Some(ref mut instance) = self.instance {
			// Silently ignore errors from set_field, as the field may not exist
			// on all model types (defensive approach for inline formsets)
			let _ = instance.set_field(field_name, value);
		}
	}

	/// Returns a reference to the underlying form.
	pub fn form(&self) -> &Form {
		&self.form
	}
	/// Returns a mutable reference to the underlying form.
	pub fn form_mut(&mut self) -> &mut Form {
		&mut self.form
	}
	/// Returns a reference to the model instance, if one exists.
	pub fn instance(&self) -> Option<&T> {
		self.instance.as_ref()
	}
}

/// Builder for creating ModelForm instances
pub struct ModelFormBuilder<T: FormModel> {
	config: ModelFormConfig,
	_phantom: PhantomData<T>,
}

impl<T: FormModel> ModelFormBuilder<T> {
	/// Creates a new `ModelFormBuilder` with default configuration.
	pub fn new() -> Self {
		Self {
			config: ModelFormConfig::default(),
			_phantom: PhantomData,
		}
	}
	/// Sets the list of fields to include in the form.
	pub fn fields(mut self, fields: Vec<String>) -> Self {
		self.config.fields = Some(fields);
		self
	}
	/// Sets the list of fields to exclude from the form.
	pub fn exclude(mut self, exclude: Vec<String>) -> Self {
		self.config.exclude = exclude;
		self
	}
	/// Overrides the widget for a specific field.
	pub fn widget(mut self, field: String, widget: crate::Widget) -> Self {
		self.config.widgets.insert(field, widget);
		self
	}
	/// Overrides the label for a specific field.
	pub fn label(mut self, field: String, label: String) -> Self {
		self.config.labels.insert(field, label);
		self
	}
	/// Overrides the help text for a specific field.
	pub fn help_text(mut self, field: String, text: String) -> Self {
		self.config.help_texts.insert(field, text);
		self
	}
	/// Build the ModelForm with the configured settings
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_forms::{ModelFormBuilder, ModelFormConfig};
	///
	/// let config = ModelFormConfig::new();
	/// let builder = ModelFormBuilder::<MyModel>::new();
	/// let form = builder.build(None);
	/// ```
	pub fn build(self, instance: Option<T>) -> ModelForm<T> {
		ModelForm::new(instance, self.config)
	}
}

impl<T: FormModel> Default for ModelFormBuilder<T> {
	fn default() -> Self {
		Self::new()
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use rstest::rstest;

	// Mock model for testing
	#[derive(Debug)]
	struct TestModel {
		id: i32,
		name: String,
		email: String,
	}

	impl FormModel for TestModel {
		fn field_names() -> Vec<String> {
			vec!["id".to_string(), "name".to_string(), "email".to_string()]
		}

		fn field_type(name: &str) -> Option<FieldType> {
			match name {
				"id" => Some(FieldType::Integer),
				"name" => Some(FieldType::Char {
					max_length: Some(100),
				}),
				"email" => Some(FieldType::Email),
				_ => None,
			}
		}

		fn get_field(&self, name: &str) -> Option<Value> {
			match name {
				"id" => Some(Value::Number(self.id.into())),
				"name" => Some(Value::String(self.name.clone())),
				"email" => Some(Value::String(self.email.clone())),
				_ => None,
			}
		}

		fn set_field(&mut self, name: &str, value: Value) -> Result<(), String> {
			match name {
				"id" => {
					if let Value::Number(n) = value {
						self.id = n.as_i64().unwrap() as i32;
						Ok(())
					} else {
						Err("Invalid type for id".to_string())
					}
				}
				"name" => {
					if let Value::String(s) = value {
						self.name = s;
						Ok(())
					} else {
						Err("Invalid type for name".to_string())
					}
				}
				"email" => {
					if let Value::String(s) = value {
						self.email = s;
						Ok(())
					} else {
						Err("Invalid type for email".to_string())
					}
				}
				_ => Err(format!("Unknown field: {}", name)),
			}
		}

		fn save(&mut self) -> Result<(), String> {
			// Mock save
			Ok(())
		}
	}

	#[rstest]
	fn test_model_form_config() {
		// Arrange
		let config = ModelFormConfig::new()
			.fields(vec!["name".to_string(), "email".to_string()])
			.exclude(vec!["id".to_string()]);

		// Assert
		assert_eq!(
			config.fields,
			Some(vec!["name".to_string(), "email".to_string()])
		);
		assert_eq!(config.exclude, vec!["id".to_string()]);
	}

	#[rstest]
	fn test_model_form_builder() {
		// Arrange
		let instance = TestModel {
			id: 1,
			name: "John".to_string(),
			email: "john@example.com".to_string(),
		};

		// Act
		let form = ModelFormBuilder::<TestModel>::new()
			.fields(vec!["name".to_string(), "email".to_string()])
			.build(Some(instance));

		// Assert
		assert!(form.instance().is_some());
	}

	#[rstest]
	fn test_model_field_names() {
		// Act
		let fields = TestModel::field_names();

		// Assert
		assert_eq!(
			fields,
			vec!["id".to_string(), "name".to_string(), "email".to_string()]
		);
	}

	#[rstest]
	fn test_save_without_instance_returns_no_instance_error() {
		// Arrange
		let config = ModelFormConfig::new();
		let mut form = ModelForm::<TestModel>::empty(config);

		// Act
		let result = form.save();

		// Assert
		assert!(result.is_err());
		let err = result.unwrap_err();
		assert!(
			matches!(err, FormError::NoInstance),
			"Expected FormError::NoInstance, got: {err}"
		);
	}
}