Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:27:53+00:00 2026-06-17T09:27:53+00:00

FormType: class BranchFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add(‘name’);

  • 0

FormType:

class BranchFormType extends AbstractType {

 public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('name');
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\MainBundle\Entity\Branch',
        ));
    }

}

In entity definition:

<field name="name" column="name" type="string" length="255"/>

there is no nullable=true and the field has required attribute when rendered.

Validation.yml:

My\MainBundle\Entity\Branch:
    properties:
        name:
            - NotBlank: ~

Does symfony find this file automatically or do I have to included somewhere? The doc just states that the form uses validation service automatically.

Controller:

    $branch = new Branch();
    $form = $this->createForm(new BranchFormType(), $branch);

    if ($request->isMethod('POST')) {

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em->persist($branch);
            $em->flush();

            return $this->redirect($this->generateUrl('view_branch'));
        }
    }

    return $this->render('MyMainBundle:Branch:create.html.twig', array(
                'form' => $form->createView()
            ));

On submit I get following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘name’
cannot be null

So isValid returns true despite the fact that name is empty and continues to persist the entity with empty values. How can isValid return true when the name is empty? ORM definition says nullable=false?`Any ideas? I have no extra validator defined. I use sf2.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:27:54+00:00Added an answer on June 17, 2026 at 9:27 am

    It looks like the validation file is in the right folder:

    src/My/MainBundle/Resources/config/validation.yml
    

    Make sure you are not using validation groups?

    Anyway, I would use Assert to see if there is something wrong with this validation file:

    Enable annotations:

    # app/config/config.yml
    framework:
        validation: { enable_annotations: true }
    

    Set up your entity:

    namespace My\MainBundle\Entity\;
    
    use Symfony\Component\Validator\Constraints as Assert;
    //...
    
    class Branch{
    
        /**
         * @Assert\NotBlank
         */
        protected $name;
    
    }
    

    Can you try this?

    Also, you can test the validation like this from your controller:

    // ...
    use My\MainBundle\Entity\Branch;
    
    public function indexAction()
    {
        $branch = new Branch();
    
        $branch->setName('');
    
        $validator = $this->get('validator');
        $errors = $validator->validate($branch);
    
        if (count($errors) > 0) {
            return new Response(print_r($errors, true));
        } else {
            return new Response('Entity is valid');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Form/Type class DemoType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name',
My form looks like this: public function buildForm(FormBuilderInterface $builder, array $options) { $factory =
//--form timesheettype---it is not entity class class TimeSheetType extends AbstractType { public function buildForm(FormBuilder
I have a form without class class ProfilesSearchType extends AbstractType { public function buildForm(FormBuilder
I have this code, but it doesn't work: $builder->add('name','text',array( 'label' => 'Due Date', ));
I am trying to test a class that extends Symfony\Component\Form\AbstractType , and having trouble
Okay, I've got the FormType described below. I use this Form class for both
controller public function indexAction(Request $request) { $user = $this->container->get('security.context')->getToken()->getUser(); $owner = $user->getId(); $first =
I have radioboxes <form method=post id=myform> <label>A</label> <input type=radio name=formtype value = 1 checked=checked>
Given these types: [DataContract] public class EntityId { [DataMember(Order = 1)] public string IdAsString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.