C0ol_Cod3r
C0ol_Cod3r

Reputation: 949

Symfony 2 / Doctrine Not Saving Any Zeros in varchar field

UPDATE

As asked for,

$NewUser = new Users();
$Form = $this->createForm(new UserType(), $NewUser, [])
              ->add('save', 'submit', ['label' => 'Save',
                                       'attr' => ['class' => 'SaveButton ftLeft'],
                                      ]);
$Form->handleRequest($request);

if ($Form->isValid()) {
   /*
      Sometimes add more data to the entity.....
   */
   $em = $this->getDoctrine()->getManager();
   $em->persist( $NewUser );
   $em->flush();
}

$FormRender = $Form->createView();
return $this->render('xxxxBundle:Users/add.html.twig',
                                    ['AddUser' => $FormRender]);

Sometimes I will add extra to the entity, on fields being set within the php code, e.g. account sign up date, but in most cases just save the entity with the form data.


Very simple issue, I have a new (ish) system and have notice that when trying to save zeros, for anything, phone number inputs, it does not save any zeros?

I am using YMAL files to control my doctrine ORM, here is how I have set it up within my User table,

    mobileNumber:
        type: string
        nullable: false
        length: 50
        options:
            fixed: false
        column: mobile_number

This does save/make the field has a varchar, and thought nothing about it until I did a random test and saw it was not saving any leading zeros?

I know you can not save leading zeros in a int field type, but this is a varchar. Also when I go into the database, and manually add a zero to that input, its fine, so I take its not the database. Something to do with how I get doctrine to save the data?

If so how do I change that? I have just been saving the data with a standard persist() and flush() commands? Not sure why it would not save the zeros? I was thinking that I had to change the YMAL type to something like phone? I have been over the docs, can not see it.

All help most welcome..

Thanks.

Upvotes: 0

Views: 979

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15686

The reason is in your comment:

User form type has the mobile number set to 'number' input.

This value is being casted to int by the Form component. Change the field type to regular string.

If you want to validate value to be only digits, use Validation component

Upvotes: 1

Related Questions