classafFormBean::FormBean

sys::Obj
  afFormBean::FormBean

Represents a Fantom type that can be rendered as a HTML form, and reconstituted back to a Fantom instance.

FormBean instances should be created by IoC.

beanType

Source

const Type beanType

The bean type this FormBean represents.

createBean

Source

virtual Obj createBean([Str:Obj?]? extraProps := null)

Creates an instance of beanType with all the field values set to the form field values. Uses afBeanUtils::BeanProperties.create().

This should only be called after validateForm().

Any extra properties passed in will also be set.

errorMsgs

Source

Str[] errorMsgs := Str[,]

You may set any extra (cross field validation) error messages here. They will be rendered along with the form field error messages.

errorSkin

Source

@Inject { optional=true }
ErrorSkin? errorSkin

The ErrorSkin used to render error messages.

fieldMsg

Source

Str? fieldMsg(FormField formField, Str key, Obj? arg1 := null, Obj? arg2 := null, Obj? arg3 := null)

Returns a message for the given field and key. Messages are looked up in the following order:

  • <bean>.<field>.<key>
  • <field>.<key>
  • <key>

And the following substitutions are made:

  • ${label} -> formField.label
  • ${value} -> formField.formValue
  • ${arg1} -> arg1.toStr
  • ${arg2} -> arg2.toStr
  • ${arg3} -> arg3.toStr

The form value is substituted for ${value} because it is intended for use by validation msgs.

Returns null if a message could not be found.

fileUploadHook

Source

|Str,InStream->File? fileUploadHook

Hook for handling file uploads. By default, an in-memory file is returned. In memory files do not require deleting.

formFields

Source

Field:FormField formFields := ... { private set }

The form fields that make up this form bean.

getByField

Source

@Operator
FormField getByField(Field field)

Returns the form field that corresponds to the given field.

formField := formBean[MyFormDetails#name]

Convenience for formBean.formFields.get(field).

getByName

Source

@Operator
FormField getByName(Str name)

Returns the form field that corresponds to the given simple field name.

formField := formBean["name"]
hasErrors

Source

Bool hasErrors()

Returns true if any form fields are in error, or if any extra error messages have been added to this

make

Source

new make(Type beanType, |This in)

Deconstructs the given form bean type to a map of FormFields.

FormBean instances should be created by IoC. Either create manually:

formBean := (FormBean) scope.build(FormBean#, [MyFormModel#])

or inject a field:

@Inject { type=MyFormModel# }
FormBean formBean
messages

Source

Str:Str messages := ...

The message map used to find strings for labels, placeholders, hints, and validation errors. Messages are read from (and overridden by) the following pod resource files:

  • <beanType.name>.properties
  • FormBean.properties

Note that property files must be in the same pod as the defining bean.

If you set a complete new map, then you probably want to call reinspectBean() to re-initialise the formfields.

msg

Source

Str? msg(Str key, Obj? arg1 := null, Obj? arg2 := null, Obj? arg3 := null)

Returns a message for the given key. Messages are looked up in the following order:

  • <bean>.<key>
  • <key>

And the following substitutions are made:

  • ${arg1} -> arg1.toStr
  • ${arg2} -> arg2.toStr
  • ${arg3} -> arg3.toStr

Returns null if a message could not be found.

reinspectBean

Source

Void reinspectBean()

Re-generates the formFields map. Useful if you've set new message properties and want them to be picked up.

renderBean

Source

virtual Str renderBean(Obj? bean := null)

Renders the form bean to a HTML form.

If the given bean is null then values are taken from the form fields. Do so if you're re-rendering a form with validation errors.

See the form field itself to render individual HTML inputs.

renderCsrfToken

Source

Bool renderCsrfToken := true

If true (default) and if SleepSafe has defined a CSRF token, then it is rendered as a hidden input when the form is rendered.

renderErrors

Source

virtual Str renderErrors()

Renders form field errors (if any) to an unordered list. Delegates to a default instance of ErrorSkin which renders the following HTML:

<div class='formBean-errors'>
    <div class='formBean-banner'>#BANNER</div>
    <ul>
        <li> Error 1 </li>
        <li> Error 2 </li>
    </ul>
</div>

To change the banner message, set a message with the key errors.banner.

renderSubmit

Source

virtual Str renderSubmit()

Renders a simple submit button.

<div class='formBean-row submitRow'>
  <input type='submit' name='formBeanSubmit' class='submit' value='label'>
</div>

The label is taken from the msg key submit.label and defaults to Submit.

renderUniqueIds

Source

Bool renderUniqueIds

If true then FormFields are rendered with a unique random prefix. Use when rendering the same form multiple times on the same page to avoid conflicting HTML IDs.

When set to true a string is automatically assigned to uniqueIdSuffix. Auto generated suffixes look like -xFFFFFFFF.

Defaults to false.

uniqueIdSuffix

Source

Str? uniqueIdSuffix

The unique suffix used for FormField IDs. Use when rendering the same form multiple times on the same page to avoid conflicting HTML IDs.

This field is set automatically when renderUniqueIds is set to true. Manually setting this field will automatically set renderUniqueIds; to false if null or true otherwise.

Default to null.

updateBean

Source

virtual Obj updateBean(Obj bean, [Str:Obj?]? extraProps := null)

Updates the given bean instance with form field values. Uses afBeanUtils::BeanProperties.

This should only be called after validateForm().

The given extra properties will also be set on the bean.

Returns the given bean instance.

validateField

Source

virtual Str? validateField(Field field, Str? formValue)

Convenience method to validate a single form field with a given value.

Note that validation is skipped for viewOnly fields and that given formValues are not trimmed.

Returns any associated formField.errMsg.

See FormField.validate().

validateForm

Source

virtual Bool validateForm(Str:Str form)

Populates the form fields with values from the given form map and performs server side validation. Error messages are saved to the form fields.

This method does not handle file uploads - use validateRequest() instead.

Returns true if all the values are valid, false if not.

It is safe to pass in HttpRequest.form() directly.

Note that all form values are trimmed before being stowed and validated.

validateHttpRequest

Source

virtual Bool validateHttpRequest()

Populates the form fields with values from the HTTP Request and performs server side validation. Error messages are saved to the form fields.

This method also handles File uploads and sets any Buf and File fields to an appropriate value. HTML Forms with file uploads must have set the attribute <form enctype="multipart/form-data" ... >

If the form content type is not multipart/form-data then processing is delegated to validateForm(). That makes this method safe to use in all situations.

In memory file are returned for file uploads, so the caller does not need to concern themselves with deleting them.

Returns true if all the values are valid, false if not.