type

DefaultFunc

Choose a field’s initial value on the server using the request, user, locale, or nearby values.

Example

go
package content

import (
  "github.com/riducms/ridu/field"
  "github.com/riducms/ridu/operation"
)

var Title = field.Text("title").Localized().DefaultFrom(initialTitle)

func initialTitle(
  ctx operation.DefaultContext,
) (operation.Value[string], error) {
  if ctx.Locale == "fr" {
    return operation.Present("Sans titre"), nil
  }
  // Present supplies the initial value; Empty would leave it unset.
  return operation.Present("Untitled"), nil
}

Arguments

ArgumentTypeDescription
ctxoperation.DefaultContextRequest information and snapshots of the values available when Ridu initializes this field. There is no current-value argument: the callback runs for omitted input.

Returns

Return Present(value), nil to supply a value; Empty[T](), nil to leave the field without a default; or an error to stop the operation. Present accepts zero, false, an empty string, or an empty list. Every supplied value must still pass the field’s validation.

(operation.Value[T], error)

How it works

Pass this callback to a field’s DefaultFrom method. T is string for Text, Textarea, Code, Email, Date, Select, and Radio; float64 for Number; bool for Checkbox; []string for MultiSelect and TextList; and []float64 for NumberList.

Ridu calls it for omitted fields when initializing new content, including children in a new object or row during an update. Explicit input, including null and empty values, does not run the callback. An ordinary edit does not refill an existing field.

Raw BeforeValidate hooks run before default initialization. A raw hook that supplies a value prevents the default from running. Later validation still checks required values, declared choices, and other field rules. Returning Empty does not satisfy Required.

Dynamic defaults are evaluated when saving on the server; they do not prefill an unsaved admin form. Config resolution, code generation, and admin builds do not call the function.

Read values already available in ctx; do not depend on another dynamic default running first. Keep the callback free of irreversible side effects because another request or a retry can call it again.

Full signature

Related types

  • DefaultContext

    The request and surrounding values available to a field’s dynamic default callback.

  • Value

    Value carries an optional logical value. Its zero value is empty. For raw input, empty means omitted and Present(store.Null()) means explicit null. For typed scalars, empty is the portable empty state, not a durable absence encoding. T must follow its own ownership contract; this carrier does not deep-clone arbitrary mutable application values.

Related guides