fieldType
fieldType(string $fieldName, string $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 an example:
$crud->fieldType('website_url', GroceryCrud::FIELD_TYPE_URL);
or:
$crud->fieldType('website_url', 'url');
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:
'string' //default
'text'
'date'
'enum'
'enum_searchable'
'datetime'
'hidden'
'timestamp'
'int'
'password'
'numeric'
'checkbox_boolean'
'url'
'email'
'color'
'dropdown'
'dropdown_search' // version 2.3.0 or later
'relational_native' // version 2.3.0 or later
'multiselect_searchable' // version 2.3.4 or later
'multiselect_native' // version 2.3.4 or later
'float' // version 2.7.1 or later
'time' // version 2.7.4 or later
'invisible' // version 2.8.0 or later
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.