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_NUMERIC);

Example 2:

$crud->fieldType('website_url', 'numeric');

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_date'
'native_datetime'
'native_time' 
'numeric'
'password'
'password_toggle'
'relational_native'
'string'
'text' 
'timestamp'

Examples

boolean

Input type for boolean (0 or 1) values.

Code example:

$crud->fieldType('active', 'boolean');

Preview:


color

Input type for HEX color values.

Code example:

$crud->fieldType('background_color', 'color');

Preview:


date

Input type for date values.

Code example:

$crud->fieldType('inserted_date', 'date');

Preview:


datetime

Input type for date and time values at the same input.

Code example:

$crud->fieldType('field_name', 'datetime');

Preview:


Native dropdown list input type with predefined options.

Code example:

$crud->fieldType('field_name', 'dropdown', ['123' => 'Option 1', '456' => 'Option 2']);

Preview:


Dropdown list with text search input type with predefined options.

Code example:

$crud->fieldType('field_name', 'dropdown_search', ['123' => 'Option 1', '456' => 'Option 2']);

Preview:


email

Input type for email. This is using the native email{" "} input type. The validation is triggered when you submit the form.

Code example:

$crud->fieldType('email_address', 'email');

Preview:

enum

Input dropdown list with predefined options. The main difference with `dropdown` is that the values of the arrays are also the ones that will be used as keys into the dropdown list.

Code example:

$crud->fieldType('field_name', 'enum', ['Option 1', 'Option 2', 'Option 3', 'Option 4']);

Preview:


enum_searchable

Input dropdown list with predefined options with a text search. The main difference with `dropdown_search` is that the values of the arrays are also the ones that will be used as keys into the dropdown list.

Code example:

$crud->fieldType('field_name', 'enum_searchable', ['Option 1', 'Option 2', 'Option 3']);

Preview:


float

Input type for numeric values. This is using the native{" "} number input type with step=.01

Code example:

$crud->fieldType('total_distance', 'float');

Preview:


hidden

Input type which is not visible in the form. Hidden fields are useful when there is a need to pass a value to the form data without the user being able to see or change it. For example a category id which is passed by session. The main difference with the `invisible` field type is that `hidden` field type is having an HTML input with type{" "} hidden in the form so it will send a value from the add and edit form. You can set the value of the hidden field by using the functions{" "} callbackAddForm or callbackEditForm.

Code example:

$crud->fieldType('category_id', 'hidden');

Preview: (the only way to see the output of this field type is to inspect the below element)


int

Input type for numeric values. Same as `numeric` field type. This is using the native number input type.

Code example:

$crud->fieldType('field_name', 'numeric');

Preview:


invisible

Input type which is not visible at all in the add/edit form or in datagrid. Invisible fields are useful when you would like to pass a value to the insert or update with callbackBeforeInsert{" "} or callbackBeforeUpdate or to add a value to an input that was generated with JavaScript. The main reason to use it is in order to not be filtered out by the validation rules.

Another useful case to use `invisible` field type is when you would like to use the data with callbackColumn. Please note that if the `invisible` field type is used on datagrid then although the column will not be visible the JSON response will include the field.

Code example:

$crud->fieldType('category_id', 'invisible');

There is no example for this field type because it is literally not a visible HTML element.


multiselect_native

Multiselect list input type using the native HTML multiselect.

Code example:

$crud->fieldType('name', 'multiselect_native', ['123' => 'Option 1', '456' => 'Option 2']);

Preview:


multiselect_searchable

Multiselect list input type with text search.

Code example:

$crud->fieldType('name', 'multiselect_searchable', ['123' => 'Option 1', '456' => 'Option 2']);

Preview:


native_date

Input type for date values, with a browser/device native date picker.

Code example:

$crud->fieldType('field_name', 'native_date');

Preview:


Input type for date values, with a browser/device native date picker. Example: `$crud->fieldType('field_name', 'native_date');` */}

native_date

Input type for date values, with a browser/device native date picker.

Code example:

$crud->fieldType('field_name', 'native_date');

Preview:


native_datetime

Input type for date and time values at the same input, with a browser/device native date and time picker.

Code example:

$crud->fieldType('field_name', 'native_datetime');

Preview:


native_time

Input type for time values with native browser/device time picker.

Code example:

$crud->fieldType('field_name', 'native_time');

Preview:


numeric

Input type for numeric values. This is using the native `number` input type.

Code example:

$crud->fieldType('field_name', 'numeric');

Preview:


password

Input type for password values.

Code example:

$crud->fieldType('field_name', 'password');

Preview:


password_toggle

Input type for password values with a toggle to show/hide the password.

Code example:

$crud->fieldType('field_name', 'password_toggle');

Preview:


string

Input type for any text value.

Code example:

$crud->fieldType('field_name', 'string');

Preview:


relational_native

There are cases that people do not need to have a searchable setRelation. As by default however the selection is searchable since this is the most common usage. For that reason, we did create a different field type that we can easily switch the setRelation to a native select input.

Example:

$crud->setRelation('office_city', 'offices', 'city');
$crud->fieldType('office_city', 'relational_native');

The above code will simply transform the select input to a native one.

Preview:


Full Example

You can find also full example below:

$crud->setTable('orders');
$crud->setSubject('Order', 'Orders');

$crud->fieldType('orderDate', 'datetime');
$crud->fieldType('requiredDate', 'datetime');
$crud->fieldType('shippedDate', 'datetime');

$crud->setRelation('customerNumber','customers','contactLastName');
$crud->fieldType('customerNumber', 'relational_native');

$output = $crud->render();