5

I start to use Symfony about 5-7 days ago, if my question is very simple, sorry, but I can't find a solution of my problem. I've created 2 form classes - UserType and ClientType. Difference between them is that in ClientType form exists few additional fields. Here is Usertype class:

namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder->add('name', 'text');
    $builder->add('username', 'text');
    $builder->add('email', 'email', array(
        'attr' => array('class'=>'email') ) );
    $builder->add('Password', 'repeated', array(
        'first_name'  => 'password',
        'second_name' => 'confirm',
        'type'        => 'password',
    ));
    $builder->add( 'roles', 'choice', array(
        'choices' => array('ROLE_SYSTEM_ADMIN' => 'System Administrator', 'ROLE_ACCOUNT_ADMIN' => 'Account Manager', 'ROLE_UPLOADER' => 'Uploader'),
        'mapped' => false ) );
    $builder->add('is_active', 'checkbox', array(
        'label'     => 'Active (user can log in)',
        'required'  => false,
    ));
    $builder->add('add_user', 'submit');
}

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver->setDefaults(array(
        'data_class' => 'Acme\Gyvfiles\GyvefileBundle\Entity\User'
    ));
  }

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

And at last ClientType class. All the same, except this part: Update:

<?php
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ClientType extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder->add('name', 'text');
    $builder->add('username', 'text');
    $builder->add('email', 'email', array(
        'attr' => array('class'=>'email') ) );
    $builder->add('Password', 'repeated', array(
        'first_name'  => 'password',
        'second_name' => 'confirm',
        'type'        => 'password',
    ));
    $builder->add('address', 'text');
    $builder->add('telephone', 'text');
    $builder->add('internalContactName', 'text');telephone
    $builder->add( 'roles', 'checkbox', array(
        'choices' => array('label' => 'Client can upload', ),
        'required'  => false ) );
    $builder->add('is_active', 'checkbox', array(
        'label'     => 'Active (user can log in)',
        'required'  => false,
    ));
    $builder->add('add_client', 'submit');
  }

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
        'data_class' => 'Acme\Gyvfiles\GyvefileBundle\Entity\Client'
    ));
  }

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

And after this steps I've used Usertype class in user controller successfully with this statement:

 $objUser = new User();
 $objForm = $this->get( 'form.factory')->create( new UserType(), $objUser ); 

But when I tried to use ClientType in Client Controller with similar syntax:

Update: ClientController

use Acme\Gyvfiles\GyvefileBundle\Entity\Permission;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Util\StringUtils;

use Acme\Gyvfiles\GyvefileBundle\Entity\Client;
use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType;

class ClientController extends Controller
{
  public function addclientAction()
  {
    $em = $this->get( 'doctrine' )->getEntityManager();
    $objClient = new Client();
    $objForm = $this->get( 'form.factory')->create( new ClientType(), $objClient );
    //$objForm->add('address', 'text');
    return $this->render( 'AcmeGyvfilesGyvefileBundle:Client:addclient.html.twig', array(
        'form'  =>  $objForm->createView(),
    ));
  }
}

appears Class not found exception.

"ClassNotFoundException: Attempted to load class "ClientType" from namespace "Acme\Gyvfiles\GyvefileBundle\Form\Type" in C:\wamp\www\Symfony\src\Acme\Gyvfiles\GyvefileBundle\Controller\ClientController.php line 22. Do you need to "use" it from another namespace?"

Can anyone help my with this problem? Thanks!

8 Answers 8

8

I just encountered the same issue. It was the wrong name of the file containing the "Type" class. It MUST be the same as the contained class.

Edit:

It's about PSR-0 autoloader. It tries to include file based on the namespace of needed class. So given Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType namespace there MUST be a ClientType class in the {pathToSymfony}/Acme/Gyvfiles/GyvefileBundle/Form/Type/ folder declared with

namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;

at the begining of the file.

That was the isue in my case. Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

this is actually the real solution to the problem. I gave credits with a mere upvote ;) I may add for the sake of discussion that you may eventually solve the namespace/path discrepancy using require once explicitily
5

I have encountered this as you described, but it went away when I took an underscore out of the class name I'm instantiating. You don't have an underscore in yours, but try changing the name anyway.

Comments

4

For me helped composer dump-autoload --optimize, because previously I executed composer dump-autoload --optimize

Comments

2

I can only assume that you are calling new ClientType or the likes in your controller? If so, you should add use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType just under your namespace declaration as otherwise it is trying to find the class ClientType in the same folder as the calling class.

If you are using namespaces then calling a class without a namespace or without it being declared as use'd means it is being called relatively (so in the same directory).

2 Comments

Thanks for your answer. Yes,I am calling new ClientType in my controller. Statements before controller are like this use Acme\Gyvfiles\GyvefileBundle\Entity\Client; use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType; . But the classnotfoundexception still occured..
Please post the full code of your controller and your ClientType class.
1

In your controller ( or in defautController-> indexAction-> of your bundle), if you use a render call with your bundle name like this: "$this->render('MyBundle:Default:index.html.twig');" you need to add a use Bundle like this : "use PathBundle\MyBundle as MyBundle at ";

Comments

0

The time I got that error my entity was like the folowing:

/**
 * InspectionPlanting
 *
 * @ORM\Table(name="inspectionplanting")
 * @ORM\Entity(repositoryClass="Pencode\ForestryBundle\Entity\ClientProfileRepository")

All I did was to change it to without including my repository:

/**
 * InspectionPlanting
 *
 * @ORM\Table(name="inspectionplanting")
 * @ORM\Entity

The last one worked. Hope this helps someone

Comments

0

When I had similar error:

Attempted to load class \"CreateImportJobHandler\" from namespace \"NG\Command\".\nDid you forget a \"use\" statement for another namespace?

I was having correct use statement in the class which was using CreateImportJobHandler, but in xml service configuration was bad.

Had to be

<parameter key="ng.command.handler.create_import_job_handler.class">NG\Command\Groups\CreateImportJobHandler</parameter>

Comments

0

In my case I had a typo in class file name: filename was Syncroniser but the classname was Synchronizer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.