Skip to main content

AdminUpdateForm

Derive Macro AdminUpdateForm 

Source
#[derive(AdminUpdateForm)]
{
    // Attributes available to this derive:
    #[update_form]
    #[update_field]
}
Expand description

Derive the AdminUpdateForm trait

§Attributes

  • update_form - struct attribute, required
    • primary_keys - the type of the primary key(s), required if no primary key is indicated in the fields
    • entity_name - name of the entity (database table), required
    • update_query - A handler which persists the entity, optional, defaults to a Postgres query constructed by the macro
  • update_field - field attribute, optional
    • required - optional, Make the field is required (only used in the frontend, losening this will require the Option<T> type in the struct)
    • default - optional, The default value for the entity
    • label - optional, The label for the input
    • form_field - optional, The form field type which is rendered in the frontend
    • primary_key - optional, Marking the field as primary key, will be used for the automatic update query and makes the field type HiddenField by default

§Example

This example shows all available attributes and values

// TODO: Create a test for it

#[derive(Clone, Debug, AdminUpdateForm, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
#[update_form(entity_name = "test", primary_keys = i32, update_query = update_entity(state).await, get_query = get_entity(state).await)]
pub struct TestEntityUpdateForm {
    #[update_field(primary_key)]
    id: i32,
    #[update_field(
        required,
        default = "Test Entity",
        label = "Entity Name",
        form_field = CustomNameSelect::new(),
    )]
    name: String,
}

async fn update_entity<S: quokka::state::State + quokka::state::ProvideState<Database>>(state: &S) -> quokka::Result<()> {
    // Update the entity here

    Ok(())
}

async fn get_entity<S: quokka::state::State + quokka::state::ProvideState<Database>>() -> quokka::Result<TestEntityUpdateForm> {
    todo!("Implement getting the entity")
}