1+ mod chrono;
12mod files;
3+ mod select;
24
35use std:: fmt:: { Debug , Display , Formatter } ;
46use std:: num:: {
@@ -8,32 +10,33 @@ use std::num::{
810
911use askama:: filters:: HtmlSafe ;
1012pub use files:: { FileField , FileFieldOptions , InMemoryUploadedFile } ;
13+ pub ( crate ) use select:: check_required_multiple;
14+ pub use select:: {
15+ SelectChoice , SelectField , SelectFieldOptions , SelectMultipleField , SelectMultipleFieldOptions ,
16+ } ;
1117
1218use crate :: auth:: PasswordHash ;
1319use crate :: common_types:: { Email , Password } ;
1420#[ cfg( feature = "db" ) ]
1521use crate :: db:: { Auto , ForeignKey , LimitedString , Model } ;
16- use crate :: form:: {
17- AsFormField , FormField , FormFieldOptions , FormFieldValidationError , FormFieldValue ,
18- FormFieldValueError ,
19- } ;
22+ use crate :: form:: { AsFormField , FormField , FormFieldOptions , FormFieldValidationError } ;
2023use crate :: html:: HtmlTag ;
2124
2225macro_rules! impl_form_field {
2326 ( $field_type_name: ident, $field_options_type_name: ident, $purpose: literal $( , $generic_param: ident $( : $generic_param_bound: ident $( + $generic_param_bound_more: ident) * ) ?) ?) => {
2427 #[ derive( Debug ) ]
2528 #[ doc = concat!( "A form field for " , $purpose, "." ) ]
2629 pub struct $field_type_name $( <$generic_param>) ? {
27- options: FormFieldOptions ,
30+ options: $crate :: form :: FormFieldOptions ,
2831 custom_options: $field_options_type_name $( <$generic_param>) ?,
2932 value: Option <String >,
3033 }
3134
32- impl $( <$generic_param $( : $generic_param_bound $( + $generic_param_bound_more) * ) ?>) ? FormField for $field_type_name $( <$generic_param>) ? {
35+ impl $( <$generic_param $( : $generic_param_bound $( + $generic_param_bound_more) * ) ?>) ? $crate :: form :: FormField for $field_type_name $( <$generic_param>) ? {
3336 type CustomOptions = $field_options_type_name $( <$generic_param>) ?;
3437
3538 fn with_options(
36- options: FormFieldOptions ,
39+ options: $crate :: form :: FormFieldOptions ,
3740 custom_options: Self :: CustomOptions ,
3841 ) -> Self {
3942 Self {
@@ -43,21 +46,22 @@ macro_rules! impl_form_field {
4346 }
4447 }
4548
46- fn options( & self ) -> & FormFieldOptions {
49+ fn options( & self ) -> & $crate :: form :: FormFieldOptions {
4750 & self . options
4851 }
4952
5053 fn value( & self ) -> Option <& str > {
5154 self . value. as_deref( )
5255 }
5356
54- async fn set_value( & mut self , field: FormFieldValue <' _>) -> std:: result:: Result <( ) , FormFieldValueError > {
57+ async fn set_value( & mut self , field: $crate :: form :: FormFieldValue <' _>) -> std:: result:: Result <( ) , $crate :: form :: FormFieldValueError > {
5558 self . value = Some ( field. into_text( ) . await ?) ;
5659 Ok ( ( ) )
5760 }
5861 }
5962 } ;
6063}
64+ pub ( crate ) use impl_form_field;
6165
6266impl_form_field ! ( StringField , StringFieldOptions , "a string" ) ;
6367
@@ -220,7 +224,7 @@ pub struct EmailFieldOptions {
220224impl Display for EmailField {
221225 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
222226 let mut tag = HtmlTag :: input ( "email" ) ;
223- tag. attr ( "name" , self . name ( ) ) ;
227+ tag. attr ( "name" , self . id ( ) ) ;
224228 tag. attr ( "id" , self . id ( ) ) ;
225229 if self . options . required {
226230 tag. bool_attr ( "required" ) ;
@@ -624,7 +628,7 @@ where
624628 }
625629}
626630
627- fn check_required < T : FormField > ( field : & T ) -> Result < & str , FormFieldValidationError > {
631+ pub ( crate ) fn check_required < T : FormField > ( field : & T ) -> Result < & str , FormFieldValidationError > {
628632 if let Some ( value) = field. value ( ) {
629633 if value. is_empty ( ) {
630634 Err ( FormFieldValidationError :: Required )
@@ -661,7 +665,7 @@ impl<T: Float> Default for FloatFieldOptions<T> {
661665impl < T : Float > Display for FloatField < T > {
662666 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
663667 let mut tag: HtmlTag = HtmlTag :: input ( "number" ) ;
664- tag. attr ( "name" , self . name ( ) ) ;
668+ tag. attr ( "name" , self . id ( ) ) ;
665669 tag. attr ( "id" , self . id ( ) ) ;
666670 if self . options . required {
667671 tag. bool_attr ( "required" ) ;
@@ -766,6 +770,7 @@ impl_float_as_form_field!(f64);
766770#[ cfg( test) ]
767771mod tests {
768772 use super :: * ;
773+ use crate :: form:: FormFieldValue ;
769774
770775 #[ test]
771776 fn string_field_render ( ) {
@@ -822,7 +827,7 @@ mod tests {
822827 assert ! ( html. contains( "required" ) ) ;
823828 assert ! ( html. contains( "minlength=\" 10\" " ) ) ;
824829 assert ! ( html. contains( "maxlength=\" 50\" " ) ) ;
825- assert ! ( html. contains( "name=\" test_name \" " ) ) ;
830+ assert ! ( html. contains( "name=\" test_id \" " ) ) ;
826831 assert ! ( html. contains( "id=\" test_id\" " ) ) ;
827832 }
828833
0 commit comments