Skip to content
You are viewing the next version (v6.7) of the documentation.
Click here to switch to the stable version (v6.6), or use the version switcher on the left to navigate between versions.

EnumField reference

Usage

The EnumField can be used to restrict string or int values to a fixed set.

Define a \BackedEnum class, use them in an Entity and restrict the values in your RDBMS.

It's not advisable to use ENUM types for integer values, as most RDBMS only support string values and use integers internally. Using a regular INT column is recommended in this case. The BackedEnum will restrict the possible values, unless the database is modified manually.

Examples

Example 1: Creating an input field from an enum

twig
<select name="payment_method">
    {% for method in PaymentMethod::cases() %}
        <option value="{{ method.value }}">{{ method.name }}</option>
    {% endfor %}
</select>

Example 2: Setting an Entity value

php
<?php

$batchOrder = new BatchOrderEntity();
$batchOrder->setPaymentMethod(PaymentMethod::PAYPAL);

Example 3: Check if a value is valid

php
<?php

$validPaymentMethod = PaymentMethod::tryFrom($userProvidedInput);

// Either check for null
if (is_null($validPaymentMethod)) {
    // The input was not a valid payment method
}

// Or check for the class
if($validPaymentMethod instanceof PaymentMethod) {
    // The input was a valid payment method
}