Symfony2:未找到类错误

时间:2014-03-14 20:59:21

标签: symfony symfony-sonata

我在这一行收到Class not found错误:

new \Placas\FrontendBundle\Form\Type\ChoiceOrTextType(),

我在Placas/FrontendBundle/Form/Type上课。

以下是代码,即时:

namespace Placas\FrontendBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Placas\FrontendBundle\Form\Type\ChoiceOrTextType;

class CategoriaAdmin extends Admin
{
    public $last_position = 0;

    private $container;
    private $positionService;

    public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
    {
        $this->positionService = $positionHandler;
    }

    protected $datagridValues = array(
        '_page' => 1,
        '_sort_order' => 'ASC',
        '_sort_by' => 'position',
    );

    protected function configureFormFields(FormMapper $formMapper)
    {
        $options = array('required' => false, 'label' => 'Imagen');
        if ($this->getSubject()->getImageName()) {
            $path = $this->getSubject()->getImageName();
            $options['help'] = '<img style="width: 300px; border: 1px solid #ccc" src="/images/' . $path . '" />';
        }

        $formMapper
            ->add('nombre', 'text', array('label' => 'Nombre', 'required' => false ))
            //->add('image', 'file', $options)
            ->add('activado', 'checkbox', array('label' => 'Activado', 'required' => false ))
            ->add('menu', new ChoiceOrTextType(), array(
    'choices'  => array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'),
    'required' => false,))
            ;

    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
        $listMapper
            ->addIdentifier('nombre')
            ->add('activado')
            ->add('position', array(), array('label' => 'Posición'))
            ->add('_action', 'actions', array(
                'label' => 'Cambio orden',
                'actions' => array(
                    'move' => array('template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig'),
                )));
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->add('move', $this->getRouterIdParameter() . '/move/{position}');
    }

}

班级:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Placas\FrontendBundle\DataTransformer\ValueToChoiceOrTextTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

class ChoiceOrTextType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('choice', 'choice', array(
                'choices' => $options['choices'] + array('Other' => 'Other'),
                'required' => false,
            ))
            ->add('text', 'text', array(
                'required' => false,
            ))
            ->addModelTransformer(new ValueToChoiceOrTextTransformer($options['choices']))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array('choices'));
        $resolver->setAllowedTypes(array('choices' => 'array'));
    }
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
    }

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
    }
    public function getParent()
    {
        return 'form';
    }

    public function getName()
    {
        return 'tags';
    }
}

$ pwd
/home/tirengarfio/workspace/mareva/src/Placas/FrontendBundle/Form/Type
$ ls
ChoiceOrTextType.php  

我只想尝试回答here

1 个答案:

答案 0 :(得分:1)

您的ChoiceOrTextType缺少namespace声明:

<?php
namespace Placas\FrontendBundle\Form\Type;

class ChoiceOrTextType
{
....
}
相关问题