-
Notifications
You must be signed in to change notification settings - Fork 12
OSS_Form_Element_DatabaseDropdown
This element combines text field and drop down functionality. In generally it allows to select value from the list or insert new one by typing it in. If there are no previous values in database available it will be displayed as simple text field. Otherwise text field will be appended with caret button which will open a drop down list and allows you to pick one of the following options or close it to leave with typed in value. If value was written in text field it will be overwritten by value from drop down list unless nothing is selected.
Database drop down element requires DQL query string to pull out options for drop down list. DQL query can be passed to constructor in options array or it can be set later by calling setChosenOptionsByDql function.
Example how to create model field using OSS_Form_Element_DatabaseDropdown:
$model = new OSS_Form_Element_DatabaseDropdown( 'model', [
'dql' => 'select p.model from \\Entities\\Phone p'
]);
$model->setRequired( false )
->setLabel( 'Model' )
->addFilter( 'StringTrim' )
->addFilter( new OSS_Filter_StripSlashes() );
$this->addElement( $model );
Another example, setting DQL using function:
$model = new OSS_Form_Element_DatabaseDropdown( 'model' );
...
$model->setChosenOptionsByDql( 'select p.model from \\Entities\\Phone p' );
Some project can have more then one database as OSS-Framework allows it. In this case you will need to specify database. Lets say or phone table is in clients database:
$model = new OSS_Form_Element_DatabaseDropdown( 'model', [
'dql' => 'select p.model from \\Entities\\Phone p',
'db' => 'clients'
]);
Or:
$model = new OSS_Form_Element_DatabaseDropdown( 'model' );
...
$model->setChosenOptionsByDql( 'select p.model from \\Entities\\Phone p', 'clients' );
It's available to pass a key-value array which. But in this case after inserting non existing value in drop down list it wouldn't be appended to an array after saving changes. It can be using to suggest something unless it reflects same field in database but array created in other place.
As example:
$options = ['Apple iPhone', 'Samsung Galaxy', 'HTC One'];
$model = new OSS_Form_Element_DatabaseDropdown( 'model', [ 'options' => $options] );
Or:
$options = ['Apple iPhone', 'Samsung Galaxy', 'HTC One'];
$model = new OSS_Form_Element_DatabaseDropdown( 'model' );
...
$model->setChosenOptions( $options );