Rename id to key and refactor from_map
This commit is contained in:
parent
29b5f5fc63
commit
064935feb1
4 changed files with 23 additions and 21 deletions
|
@ -33,7 +33,7 @@ pub enum CustomFieldType {
|
|||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CustomField {
|
||||
pub id: String,
|
||||
pub key: String,
|
||||
pub label: String,
|
||||
#[serde(default)]
|
||||
pub required_feedback: Option<String>,
|
||||
|
|
28
src/forms.rs
28
src/forms.rs
|
@ -34,6 +34,7 @@ pub struct ContactForm {
|
|||
}
|
||||
|
||||
impl ContactForm {
|
||||
/// Build the form from the form map.
|
||||
pub fn from_map(
|
||||
mut map: HashMap<String, String>,
|
||||
custom_fields: &Vec<CustomField>,
|
||||
|
@ -43,21 +44,22 @@ impl ContactForm {
|
|||
.context("id missing in the submitted form!")?
|
||||
.parse()
|
||||
.context("Failed to parse the submitted id!")?;
|
||||
let captcha_answer = map
|
||||
.remove("captcha_answer")
|
||||
.context("captcha_answer missing in the submitted form!")?;
|
||||
let name = map
|
||||
.remove("name")
|
||||
.context("name missing in the submitted form!")?;
|
||||
let email = map
|
||||
.remove("email")
|
||||
.context("email missing in the submitted form!")?;
|
||||
|
||||
// Remove an item with the given key from the map.
|
||||
let mut remove = |key| {
|
||||
map.remove(key)
|
||||
.with_context(|| format!("{} missing in the submitted form!", key))
|
||||
};
|
||||
|
||||
// Default fields.
|
||||
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());
|
||||
for field in custom_fields.iter() {
|
||||
custom.push(
|
||||
map.remove(&field.id)
|
||||
.with_context(|| format!("{} missing in the submitted form!", &field.id))?,
|
||||
);
|
||||
custom.push(remove(&field.key)?);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
|
|
|
@ -78,9 +78,9 @@ pub async fn submit(
|
|||
State(config): State<Arc<StateConfig>>,
|
||||
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
|
||||
State(mailer): State<Arc<Mailer>>,
|
||||
Form(form): Form<HashMap<String, String>>,
|
||||
Form(map): Form<HashMap<String, String>>,
|
||||
) -> 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
|
||||
.lock()
|
||||
|
|
|
@ -38,22 +38,22 @@
|
|||
{% let value = persistant_field_contents.custom[loop.index0] %}
|
||||
{% 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 %}
|
||||
{% when config::CustomFieldType::Text %}
|
||||
<input type="text"
|
||||
class="form-control"
|
||||
name="{{ custom_field.id }}"
|
||||
id="{{ custom_field.id }}"
|
||||
name="{{ custom_field.key }}"
|
||||
id="{{ custom_field.key }}"
|
||||
value="{{ value }}"
|
||||
{% if required %}required{% endif %}>
|
||||
{% when config::CustomFieldType::Textarea with { rows } %}
|
||||
<textarea rows="{{ rows }}"
|
||||
style="resize: none"
|
||||
class="form-control"
|
||||
name="{{ custom_field.id }}"
|
||||
id="{{ custom_field.id }}"
|
||||
name="{{ custom_field.key }}"
|
||||
id="{{ custom_field.key }}"
|
||||
{% if required %}required{% endif %}>{{ value }}</textarea>
|
||||
{% endmatch %}
|
||||
|
||||
|
|
Loading…
Reference in a new issue