ValidForm::OptionSet (Class)

In: ValidForm.rb
ValidForm_html.rb
Parent: Object

Represents a collection of related options. These may be mutually exclusive, (e.g. a set of radio buttons, or a pull-down menu) or multiple may be chosen at once (e.g. a set of checkboxes, or a list of selectable options).

Each option set keeps track of one or more ::Option objects, added through ::Container#add_fields.

If the value is set for the option set when it is created, any options passed to add_fields with those values will be chosen automatically. This feature only occurs the first time add_fields is called.

See also ::Option#new for information on pre-choosing an option.

See also ::RadioSet and ::CheckboxSet, subclasses used to specify the desired interface rendering.

Methods

External Aliases

add_fields -> real_add_fields

Attributes

allowCustom  [RW]  Truth value indicating if a value other than those available through the options may be entered; false by default. Not yet supported.
chooseMultiple  [RW]  Truth value indicating if the user may choose more than one option in the set; false by default.
options  [R]  Array of all options currently tracked by the set. Direct modifications to this array may cause unexpected errors. Use ::Container#add_fields to add new options to the set.

Included Modules

Public Class methods

(See ::Field#new for information on the parameters.)

Option sets are added to the form as a single field:

  search = ValidForm.new('search.rhtml','GET')
  search.add_fields(
     ValidForm::Text.new('q','q',nil,'Find:'),
     ValidForm::OptionSet.new('sections',nil,nil,'Look In:').add_fields(
             ValidForm::Option.new('s1','slashdot.org','/.'),
             ValidForm::Option.new('s2','google.com','Google'),
             ValidForm::Option.new('s3','apple.com','Apple')
     )
  )
  search.add_fields( ValidForm::Submit.new('fsubmit',nil,nil,'Go') )

Public Instance methods

Returns an HTML representation of the field itself. See also ::Field#to_html and ::CheckboxSet#field_tohtml and ::Radioset#field_tohtml.

See ValidForm#to_html for a description on how to use (or ignore) the &block parameter.

Unchooses every option in the set (except for an optional optionToSkip option which may be supplied).

(Primarily for internal use by the library to enforce a unique choice when chooseMultiple is false.)

Note that this does not choose optionToSkip (if supplied); that option’s chosen state is left unaffected.

In addition to the validation rules listed in ::Field#validate, option sets have two additional rules which may be set:

minchosen:An integer representing the minimum number of options which must be chosen to be valid.
maxchosen:An integer representing the maximum number of options which may be chosen to be valid.

To ensure that (at least) one option is chosen for the set, either the required or minchosen validation rule may be used.

If only one option is chosen, returns the value of that option. If more than one option is chosen, returns an Array of values. If no options are chosen, returns nil. See also values.

optionValue:May be a single value, a comma-delimited String of values, or an Array of values.

Chooses the option(s) whose value is in optionValue; unchooses all other options in the set.

optionValue:May be a single value, a comma-delimited String of values, or an Array of values.

Chooses the option(s) whose value is in optionValue; unchooses all other options in the set.

All values are converted to strings and compared as strings (when ValidForm_html is included).

Returns an Array of the value of every option in the set that is chosen.

If no option in the set is chosen, a zero-length Array is returned; if only one option is chosen, a single-element Array is returned. See also value.

All values are returned as strings (when ValidForm_html is included).

Returns an Array of the value of every option in the set that is chosen.

If no option in the set is chosen, a zero-length Array is returned; if only one option is chosen, a single-element Array is returned. See also value.

[Validate]