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:
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' 'url' 'email' 'color' 'dropdown' 'dropdown_search' 'relational_native' 'multiselect_searchable' 'multiselect_native' 'float' 'native_time' 'invisible'
In order to change the field type into a float number is as simple as one line of code:
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' ]);
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' ]);
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' ]);
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.