Stworzyłem sobie typ Enum z wartościami:
<?php namespace App\Form\Enum; enum MeetName: string { case MECZ1 = 'Mecz 1'; case MECZ2 = 'Mecz 2'; }
<?php namespace App\Form; use Symfony\Component\Form\Extension\Core\Type\EnumType; use App\Form\Enum\MeetName; class MeetType extends AbstractType { public function buildForm(FormBuilderInterface $builder, http://www.php.net/array $options) { $builder ->add('name' , null, ['label' => 'Nazwa']) ->add('name' , EnumType::class, [ 'class' => MeetName::class ])
Odpalałeś już czyszczenie cache?
odpaliłem i przy odpaleniu pojawia się ten sam błąd:
Expected to find class "App\Form\Enum\MeetName" in file "C:\xampp\htdocs\ligatyperow2\src/Form\Enum\MeetName.php" while importing services from resource "../src/*", but it
was not found! Check the namespace prefix used with the resource.
Pokaż jak wyglądają pliki: `services.yaml` i `composer.json`. Pokaż też strukturę katalogów.
i napisz jaka wersje php masz
Wersja PHP:
C:\Users\czechdam>php -version
PHP 7.4.29 (cli) (built: Apr 12 2022 20:21:18) ( ZTS Visual C++ 2017 x64 )
Copyright © The PHP Group
Zend Engine v3.4.0, Copyright © Zend Technologies
Pracuje na Windows i Symfony wersja 4.4
Struktura katalogów:
główny projekt:
.
..
.env
.gitignore
.idea
bin
composer-setup.php
composer.json
composer.lock
config
nbproject
phpunit.xml.dist
public
src
symfony
symfony.lock
templates
tests
translations
var
vendor
cd src:
.
..
Command
Controller
DataFixtures
Entity
EventListener
Exception
Form
Kernel.php
Manager
Migrations
Repository
Security
Service
Twig
Util
cd Form:
.
..
ChangePasswordType.php
CommentType.php
Enum
LeagueType.php
MeetType.php
TeamType.php
UserType.php
cd Enum:
.
..
MeetName.php
plik services.yaml:
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\EventListener\ActivityListener:
arguments:
- '@security.token_storage'
- '@doctrine.orm.entity_manager'
tags:
- { name: 'kernel.event_listener', event: 'kernel.controller', method: 'onCoreController' }
App\EventListener\ResultListener:
tags:
- { name: doctrine.event_listener, event: postUpdate }
plik composer.json:
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1",
"stof/doctrine-extensions-bundle": "^1.3",
"symfony/asset": "4.3.*",
"symfony/console": "4.3.*",
"symfony/dotenv": "4.3.*",
"symfony/expression-language": "4.3.*",
"symfony/flex": "^1.3.1",
"symfony/form": "4.4.2",
"symfony/framework-bundle": "4.4.0",
"symfony/http-client": "4.4.*",
"symfony/intl": "4.4.*",
"symfony/monolog-bundle": "^3.1",
"symfony/orm-pack": "^1.0",
"symfony/process": "4.3.*",
"symfony/security-bundle": "4.4.0",
"symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/translation": "4.4.*",
"symfony/twig-bundle": "4.4.*",
"symfony/validator": "4.4.*",
"symfony/web-link": "4.3.*",
"symfony/yaml": "4.3.*"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4",
"symfony/debug-bundle": "^4.4",
"symfony/maker-bundle": "^1.12",
"symfony/phpunit-bridge": "^5.0",
"symfony/stopwatch": "^4.4",
"symfony/test-pack": "*",
"symfony/var-dumper": "^4.4",
"symfony/web-profiler-bundle": "^4.4.2",
"symfony/web-server-bundle": "4.3.*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true,
"allow-plugins": {
"symfony/flex": true
}
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.2"
}
}
}
Enumy są od wersji PHP 8.1 dostępne. Ty masz 7.4 - dlatego to nie działa.
Przepisz klasę enum na consty.
Możesz też użyć jednej z dostępnych bibliotek:
- https://github.com/myclabs/php-enum
- https://github.com/spatie/enum
Jest ich więcej, ale z tego co czytałem, to powyższe są najpopularniejsze
Po pierwsze używaj bbcodes, po drugie skoro:
Dzięki Panowie. Faktycznie zabrałem się do tego jak pies do jeża.
Zrobiłem na razie tak:
$meetsName = http://www.php.net/array ( 'Mecz 1' => '1', 'Mecz 2' => '2', 'Mecz 3' => '3', 'Mecz 4' => '4', 'Mecz 5' => '5', 'Mecz 6' => '6', 'Mecz 7' => '7', 'Mecz 8' => '8', 'Mecz 9' => '9', 'Mecz 10' => '10' ); $builder ->add('name' , null, ['label' => 'Nazwa']) ->add('name' , ChoiceType::class, [ 'choices' => $meetsName ])
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)