webCOMAND

form::choices()

Return an array of objects that represent the choices for the specified field.  Only Data Type fields with Choices set or a single or list forward reference field will return an array.

Prototype

array choices(mixed $field, array $options = [])

Parameters

  • field - A Content Type Field object or Field Identifier string.
  • options - The following optional key/value pairs:
    • option_title - Field Identifier of the field to use when this field will produce a drop-down.  If the options are based on a content type (ie. a forward reference field or Data Type with Location), any field of the corresponding Content Type can be used.  If the options are based on  name to use
    • option_value - Similar to option_title above, but to determine the value.
    • custom_options - An array of associative arrays each with optional 'title', 'value', and 'object' keys.  These custom options will be merged into the standard choices alphabetically, unless custom_options_merge is specified.  If a title or value is not provided, an empty string will be assumed.
    • custom_options_merge - Change how custom_options are merged into the standard choices.  Options are 'prepend', 'append' and 'alpha' (default).

Return

An array of objects with the fields:

  • title - Title for the choice.
  • value - Value for the choice, which may be a string for Data Type fields, or OID for forward reference fields.
  • object - Choice object for Data Type fields, or Object for forward reference fields.

Example

$repo = comand::repo();
$contact = $repo->new_object('Contact');
$form = new \io_comand_web\form($contact);
$options = '<select>';
foreach($form->choices('ContactType') as $choice) {
    $options .=
        '<option value="' . htmlspecialchars($choice->value) . '">' .
        $choice->title .
        '</option>';
}
$options .= '</select>';

Related

input(), field()