Using the addField() function we can generate selects by simply parsing specific values in an array or specific settings to generate it from database values.
The minimum parameters to pass are:
addField([
'type'=> 'select',
'label'=> 'Label Title',
'name'=> 'colName', //optional
'placeholder'=> 'Select an option', //optional - default: Select an option
'value'=> 'selected value', //optional, without one a "Select an option" option appears
'options' => [
['name' => 'First choice', 'value' => 1],
['name' => 'Second choice', 'value' => 2],
['name' => 'Third choice', 'value' => 3]
]
]);
If we want to get the options data from a database our array needs the following keys:
addField([
'type'=> 'select',
'label'=> 'Label Title',
'name'=> 'colName', //optional
'showAll' => true, //optional, false by default
'showAllTxt' => 'Show All Items', // optional, default Show All when Show All is true or Select an option (disabled) when no value is selected
'value'=> 'selected value', //optional, without one a "Select an option" option appears
'optionsdb'=> [ //required if options is not used
'table' => 'TABLE NAME', //required
'op_name' => 'COL_NAME', //required
'op_val' => 'COL_NAME', //required
'where' => 'WHERE STATEMENT', //optional
'order' => 'ORDER STATEMENT' //optional
]
]);
For example:
'optionsdb'=> [ "table" => "CLIENT_TYPE", "op_name" => "type_$mainLang", "op_val" => "CTID", "where" => "type_$mainLang != '' ", "order" => "order by type_$mainLang" ];
All the options
addField([
'type'=> 'select',
'label'=> 'Label Title',
'name'=> 'colName', //optional
'placeholder'=> 'Select an option', //optional - default: Select an option
'id' => 'selID', //optional, name is used as id if id is null
'value'=> 'selected value', //optional, without one a "Select an option" option appears
'attributes' => 'required', //optional
'style' => 'border:1px solid red', //optional
'showAll' => true, //optional, false by default
'showAllTxt' => 'Show All Items', // optional, default Show All when Show All is true or Select an option (disabled) when no value is selected
'class' => '', //optional
'options'=> array(), //optional, required if optionsdb is not used
'optionsdb'=> [ //required if options is not used
'table' => 'TABLE NAME', //required
'op_name' => 'COL_NAME', //required
'op_val' => 'COL_NAME', //required
'where' => 'WHERE STATEMENT', //optional
'order' => 'ORDER STATEMENT' //optional
]
]);
(The old way) You can also parse a CSV string with : as the delimiter between names and values.
$selValues = "value:name,value:name,value:name"
$selValues = 'VI:Visa,MC:Mastercard,AMEX:American Express';
addField('select','CC Provider','CC_PROVIDER:',$selValues);
Optional relationships: preselect, change and clear
For an optional database relationship, use showAll => true with an empty-choice label such as Unassigned. On Edit, pass the loaded relationship value; on Add, omit value to begin unassigned. Do not mark the field required. Selecting the empty option saves SQL NULL through the existing native INSERT/UPDATE handling; no custom AJAX is needed.
addField([
'type' => 'select', 'label' => 'Category', 'name' => 'CCID',
'showAll' => true, 'showAllTxt' => 'Unassigned',
'value' => $db_items->result(0, 'CCID'), // Edit only; omit on Add.
'optionsdb' => [
'table' => 'CLIENT_CATEGORIES', 'op_name' => 'TYPE', 'op_val' => 'CCID',
'order' => 'pos, CCID'
]
]);
This example uses the existing KAS Core nullable CLIENTS.CCID relationship and positioned category lookup. Use the actual schema of the owning form. Native lookup ordering is separate from table-row sorting. Verify assignment, preselection, clearing and reload persistence through the normal form submit workflow.