fieldType
fieldType(string $fieldName, string|ModelFieldType $fieldType[, array $permittedValues[, array $options]])
The are many cases that the default field type of the database is not the required or that the field type is a simple varchar although we need to have a specific type for add/edit/view. In that case you can use the function fieldType
to force the field as that kind of type. Have in mind that this function was renamed from changeFieldType
for simplicity.
Below you can see some examples of how you can use the fieldType
function:
Example 1:
$crud->fieldType('website_url', GroceryCrud::FIELD_TYPE_URL);
Example 2:
$crud->fieldType('website_url', 'url');
Example 3:
// At the beginning of the file
use GroceryCrud\Core\Model\ModelFieldType;
...
$myField = new ModelFieldType();
$myField->setDataType(GroceryCrud::FIELD_TYPE_NUMERIC);
$crud->fieldType('date_birth_year', $myField);
You can find all the types that exists from grocery crud by typing GroceryCrud:FIELD_
if you are using an editor that it is recognizing the field types then you should see all the list of available fields. To make things more simple to you we have the full list (that will be always up to date) in case you need to copy-paste it really fast. Although it is strongly suggested to use the constants of GroceryCrud class for that:
'boolean'
'color'
'date'
'datetime'
'dropdown'
'dropdown_search'
'email'
'enum'
'enum_searchable'
'float'
'hidden'
'int'
'invisible'
'multiselect_native'
'multiselect_searchable'
'native_time'
'numeric'
'password'
'password_toggle' // Available from version 3.1.9 and later
'relational_native'
'string' // default
'text'
'timestamp'
'url'
Examples
float
In order to change the field type into a float number is as simple as one line of code:
$crud->fieldType('total_distance', 'float');
dropdown_search
A dropdown list that it is also searchable.
// Example by referring to a database id (e.g.12,13, 14... e.t.c.)
$crud->fieldType('contact_title', 'dropdown_search', [
'12' => 'Master',
'13' => 'Mr',
'14' => 'Miss',
'15' => 'Mrs',
'16' => 'Missus',
'17' => 'Ms',
'18' => 'Mx'
]);
// Example by adding the actual value
$crud->fieldType('contact_title', 'dropdown_search', [
'Master' => 'Master',
'Mr' => 'Mr',
'Miss' => 'Miss',
'Mrs' => 'Mrs',
'Missus' => 'Missus',
'Ms' => 'Ms',
'Mx' => 'Mx'
]);
multiselect_native
A multiselect HTML field. More specifically the native <select multiple="multiple"> On insert and update, all the values are inserted to the same field separated by comma.
$crud->fieldType('gift_category', 'multiselect_native', [
'baby' => 'Baby',
'beauty' => 'Beauty',
'stripbooks' => 'Books',
'automotive' => 'Car & Motorbike',
'popular' => 'CDs & Vinyl',
'classical' => 'Classical Music',
'clothing' => 'Clothing',
'computers' => 'Computers & Accessories',
'outdoor' => 'Garden & Outdoors',
'gift-cards' => 'Gift Cards'
]);
multiselect_searchable
A multiselect field with the ability to search. On insert and update, all the values are inserted to the same field separated by comma.
$crud->fieldType('gift_category', 'multiselect_searchable', [
'baby' => 'Baby',
'beauty' => 'Beauty',
'stripbooks' => 'Books',
'automotive' => 'Car & Motorbike',
'popular' => 'CDs & Vinyl',
'classical' => 'Classical Music',
'clothing' => 'Clothing',
'computers' => 'Computers & Accessories',
'outdoor' => 'Garden & Outdoors',
'gift-cards' => 'Gift Cards'
]);
relational_native
There are cases that people doesn't like or doesn't need to have a searchable setRelation. As by default however the selection is searchable (most common usage is with a search) we did create a different field type that we can easy switch the setRelation to a native select input. With the below example things will be more clear:
$crud->setRelation('officeCode', 'offices', 'city');
$crud->fieldType('officeCode', 'relational_native');
The above code will simply transform the select input to a native one.
Full Example with Demo
You can find a full example below:
$crud->setTable('orders');
$crud->setSubject('Order', 'Orders');
$crud->unsetAdd();
$crud->fieldType('orderDate', 'datetime');
$crud->fieldType('requiredDate', 'datetime');
$crud->fieldType('shippedDate', 'datetime');
$crud->setRelation('customerNumber','customers','contactLastName');
$output = $crud->render();
As you will also notice at the below example the fields: 'orderDate', 'requiredDate', 'shippedDate' are now a datetime field. GroceryCRUD is recognizing if the browser is supporting the datetime native input and if not it is falling back to a jQuery plugin (in our case this is the jQuery UI for datetime). You can see the difference by opening the page in chrome and the same page in firefox.