Skip to main content

AdminCreateForm

Derive Macro AdminCreateForm 

Source
#[derive(AdminCreateForm)]
{
    // Attributes available to this derive:
    #[create_form]
    #[create_field]
}
Expand description

Derive the AdminCreateForm trait

§Attributes

  • create_form - struct attribute, required
    • entity_name - name of the entity (database table), required
    • create_query - A handler which persists the entity, optional, defaults to a Postgres query constructed by the macro
  • create_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

§Example

This example shows all available attributes and values

// TODO: Create a test for it

#[derive(Clone, Debug, AdminCreateForm, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
#[create_form(entity_name = "test", create_query = create_entity(state).await)]
pub struct TestEntityCreateForm {
    #[create_field(
        required,                             // Whether the field is required or not
        default = "Test Entity",              // The default value for the entity
        label = "Entity Name",                // The label for the input
        form_field = CustomNameSelect::new(), // The form field type which is rendered in the frontend
    )]
    name: String,
}

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

    Ok(())
}