Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][Symfony4][SF4] EnumType in Form field
Forum PHP.pl > Forum > PHP > Frameworki
damianooo
Stworzyłem sobie typ Enum z wartościami:

  1. <?php
  2.  
  3. namespace App\Form\Enum;
  4.  
  5. enum MeetName: string {
  6. case MECZ1 = 'Mecz 1';
  7. case MECZ2 = 'Mecz 2';
  8. }


dodałem go sobie do formularza :

  1. <?php
  2. namespace App\Form;
  3.  
  4. use Symfony\Component\Form\Extension\Core\Type\EnumType;
  5. use App\Form\Enum\MeetName;
  6.  
  7. class MeetType extends AbstractType
  8. {
  9. public function buildForm(FormBuilderInterface $builder, array $options)
  10. {
  11. $builder
  12. ->add('name' , null, ['label' => 'Nazwa'])
  13. ->add('name' , EnumType::class, [
  14. 'class' => MeetName::class
  15. ])


i wydawało mi się że dobrze dodałem , zgodnie z dok.:
https://symfony.com/doc/current/reference/f...types/enum.html

ale niestety mam taki 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 in C:\xampp\htdocs\ligatyperow2\config/services.yaml (which is loaded in resource "C:\xampp\htdocs\ligatyperow2\config/services.yaml").

co mam nie tak ?
Salvation
Odpalałeś już czyszczenie cache?
Kod
php bin/console c:c
Salvation
Pokaż jak wyglądają pliki: `services.yaml` i `composer.json`. Pokaż też strukturę katalogów.
ohm
i napisz jaka wersje php masz
damianooo
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:

https://symfony.com/doc/current/best_practi...d-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 }"># 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_practi...d-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"
}
}
}
Salvation
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

Cytat(ohm @ 24.10.2022, 23:45:37 ) *
i napisz jaka wersje php masz

Skąd wiedziałeś, że wersja PHP może być przyczyną?
ohm
Cytat(Salvation @ 25.10.2022, 09:37:21 ) *
Skąd wiedziałeś, że wersja PHP może być przyczyną?



Ścieżki się zgadzały, nazwy klas itp, ale był problem z "Expected to find class", no i faktycznie klasy żadnej tam nie ma, tylko enum którego php nie rozumie, więc wydedukowałem że to może być problem. Swoją drogą, z tego co widzę to EnumType w symfony pojawił się w wersji 5.4 także to też będzie pewnie generowało problemy.
com
Po pierwsze używaj bbcodes, po drugie skoro:
Cytat
Symfony wersja 4.4


To czemu wgl przeglądasz dokumentacje dla 5.4 i więcej? smile.gif

>W 4.4 nie ma EnumTypes wcale wink.gif
damianooo
Dzięki Panowie. Faktycznie zabrałem się do tego jak pies do jeża.

Zrobiłem na razie tak:

  1. $meetsName = array (
  2. 'Mecz 1' => '1',
  3. 'Mecz 2' => '2',
  4. 'Mecz 3' => '3',
  5. 'Mecz 4' => '4',
  6. 'Mecz 5' => '5',
  7. 'Mecz 6' => '6',
  8. 'Mecz 7' => '7',
  9. 'Mecz 8' => '8',
  10. 'Mecz 9' => '9',
  11. 'Mecz 10' => '10'
  12. );
  13.  
  14. $builder
  15. ->add('name' , null, ['label' => 'Nazwa'])
  16. ->add('name' , ChoiceType::class, [
  17. 'choices' => $meetsName
  18. ])


Muszę chyba jak najszybciej przenieść się na wersję 5.4 albo nawet 6.0
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.