Rename id to key and refactor from_map

This commit is contained in:
Mo 2023-02-25 21:14:58 +01:00
parent 29b5f5fc63
commit 064935feb1
4 changed files with 23 additions and 21 deletions

View file

@ -33,7 +33,7 @@ pub enum CustomFieldType {
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct CustomField { pub struct CustomField {
pub id: String, pub key: String,
pub label: String, pub label: String,
#[serde(default)] #[serde(default)]
pub required_feedback: Option<String>, pub required_feedback: Option<String>,

View file

@ -34,6 +34,7 @@ pub struct ContactForm {
} }
impl ContactForm { impl ContactForm {
/// Build the form from the form map.
pub fn from_map( pub fn from_map(
mut map: HashMap<String, String>, mut map: HashMap<String, String>,
custom_fields: &Vec<CustomField>, custom_fields: &Vec<CustomField>,
@ -43,21 +44,22 @@ impl ContactForm {
.context("id missing in the submitted form!")? .context("id missing in the submitted form!")?
.parse() .parse()
.context("Failed to parse the submitted id!")?; .context("Failed to parse the submitted id!")?;
let captcha_answer = map
.remove("captcha_answer") // Remove an item with the given key from the map.
.context("captcha_answer missing in the submitted form!")?; let mut remove = |key| {
let name = map map.remove(key)
.remove("name") .with_context(|| format!("{} missing in the submitted form!", key))
.context("name missing in the submitted form!")?; };
let email = map
.remove("email") // Default fields.
.context("email missing in the submitted form!")?; let captcha_answer = remove("captcha_answer")?;
let name = remove("name")?;
let email = remove("email")?;
// Custom fields.
let mut custom = Vec::with_capacity(custom_fields.len()); let mut custom = Vec::with_capacity(custom_fields.len());
for field in custom_fields.iter() { for field in custom_fields.iter() {
custom.push( custom.push(remove(&field.key)?);
map.remove(&field.id)
.with_context(|| format!("{} missing in the submitted form!", &field.id))?,
);
} }
Ok(Self { Ok(Self {

View file

@ -78,9 +78,9 @@ pub async fn submit(
State(config): State<Arc<StateConfig>>, State(config): State<Arc<StateConfig>>,
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>, State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
State(mailer): State<Arc<Mailer>>, State(mailer): State<Arc<Mailer>>,
Form(form): Form<HashMap<String, String>>, Form(map): Form<HashMap<String, String>>,
) -> Result<Response, AppError> { ) -> Result<Response, AppError> {
let form = ContactForm::from_map(form, &config.custom_fields)?; let form = ContactForm::from_map(map, &config.custom_fields)?;
let right_captcha_answer = captcha_solutions let right_captcha_answer = captcha_solutions
.lock() .lock()

View file

@ -38,22 +38,22 @@
{% let value = persistant_field_contents.custom[loop.index0] %} {% let value = persistant_field_contents.custom[loop.index0] %}
{% let required = custom_field.required_feedback.is_some() %} {% let required = custom_field.required_feedback.is_some() %}
<label for="{{ custom_field.id }}" class="form-label">{{ custom_field.label }}</label> <label for="{{ custom_field.key }}" class="form-label">{{ custom_field.label }}</label>
{% match custom_field.field_type %} {% match custom_field.field_type %}
{% when config::CustomFieldType::Text %} {% when config::CustomFieldType::Text %}
<input type="text" <input type="text"
class="form-control" class="form-control"
name="{{ custom_field.id }}" name="{{ custom_field.key }}"
id="{{ custom_field.id }}" id="{{ custom_field.key }}"
value="{{ value }}" value="{{ value }}"
{% if required %}required{% endif %}> {% if required %}required{% endif %}>
{% when config::CustomFieldType::Textarea with { rows } %} {% when config::CustomFieldType::Textarea with { rows } %}
<textarea rows="{{ rows }}" <textarea rows="{{ rows }}"
style="resize: none" style="resize: none"
class="form-control" class="form-control"
name="{{ custom_field.id }}" name="{{ custom_field.key }}"
id="{{ custom_field.id }}" id="{{ custom_field.key }}"
{% if required %}required{% endif %}>{{ value }}</textarea> {% if required %}required{% endif %}>{{ value }}</textarea>
{% endmatch %} {% endmatch %}