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!
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With