Forms.vala 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Statum;
  2. namespace ClankerSaddle {
  3. /**
  4. * Form-reading helpers shared by the app's actions: a missing form or
  5. * field is an empty value, and empty optional fields read as null
  6. * (meaning "leave unchanged").
  7. */
  8. public class Forms : Object {
  9. /** The trimmed value of form field `name`, or "" when absent. */
  10. public static string field(StatumRequest request, string name) {
  11. return request.form != null ? (request.form.get_field(name) ?? "").strip() : "";
  12. }
  13. /** The raw (untrimmed) value of form field `name`, or "". */
  14. public static string field_raw(StatumRequest request, string name) {
  15. return request.form != null ? (request.form.get_field(name) ?? "") : "";
  16. }
  17. /**
  18. * The trimmed value of form field `name` as a nullable: null when
  19. * the form or field is absent or empty.
  20. */
  21. public static string? optional(StatumRequest request, string name) {
  22. var value = field(request, name);
  23. return value.length > 0 ? value : null;
  24. }
  25. /** The integer value of form field `name`, or `fallback`. */
  26. public static int64 integer(StatumRequest request, string name, int64 fallback) {
  27. var value = field(request, name);
  28. return value.length > 0 ? int64.parse(value) : fallback;
  29. }
  30. }
  31. }