2

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.

3
  • Must be a typo in filename CompanyTransferConstraint.php or file permissions issue, nothing more. Commented Sep 1, 2015 at 14:04
  • 1
    thanks HPierce, that was the issue, I need a break Commented Sep 1, 2015 at 14:10
  • @neoman, since it actually fixed the problem I've deleted the comment and added it as an answer instead. Commented Sep 1, 2015 at 14:20

1 Answer 1

1

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.

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

1 Comment

thanks again that was very helpful.I accepted the answer.

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.