Reputation: 41
My custom constraint class is not found and I don't know where is the problem.
this is the error
Attempted to load class "CompanyTransferConstraint" from namespace "FM\FMBundle\Validator\Constraints". Did you forget a "use" statement for another namespace? 500 Internal Server Error - ClassNotFoundException
the constraint class and the validator class are both located in src/FM\FMBundle\Validator\Constraints
here is my code :
CompanyTransferConstraint
<?php
namespace FM\FMBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class CompanyTransferConstraint extends Constraint
{
public $message = 'You do not have the requested amount';
public function validatedBy()
{
return get_class($this).'Validator';
}
}
CompanyTransferConstraintValidator
<?php
namespace FM\FMBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CompanyTransferConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if ($value>200) {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}
the Form Type
<?php
namespace FM\FmBundle\Form\Type\Dash;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use FM\FMBundle\Validator\Constraints\CompanyTransferConstraint;
use Symfony\Component\Validator\Constraints\Type;
class CompanyTransferType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('totalAmount','integer',
array(
'constraints' =>
array(
new NotBlank(),
new Type('integer'),
new CompanyTransferConstraint(),
)
)
)
->add('save','submit')
;
}
public function getName()
{
return 'fm_fmbundle_company_transfer';
}
}
I also tested it with a User entity like this
/*
...
*/
use FM\FMBundle\Validator\Constraints as FmAssert;
class User extends BaseUser
{
/*
.....
*/
/**
* @FmAssert\CompanyTransferConstraint
*/
protected $phone;
/*
.....
*/
}
and I get this error
AnnotationException in AnnotationException.php line 54: [Semantical Error] The annotation "@FM\FMBundle\Validator\Constraints\CompanyTransferConstraint" in property FM\FmBundle\Entity\User::$phone does not exist, or could not be auto-loaded.
Upvotes: 2
Views: 2715
Reputation: 7409
You need to make sure that your capitalization on your namespaces is consistent.
Your CompanyTransferConstraint and CompanyTransferConstraintValidator use:
namespace FM\FmBundle\Form\Type\Dash;
Your Form Type uses:
namespace FM\FMBundle\Validator\Constraints;
The difference in capitalization in FMBundle
appears to be causing problems. The other error that you've edited in seems to suffer from the same problem.
Upvotes: 1