andrianoid
andrianoid

Reputation: 51

symfony 2 form string missing first character

I have an app using symfony 2.2 that has a file upload form, and another 'reportbuilder' form.

The issue I'm having is that when any of my input field values begin with a 'c', that letter is dropped. So if the field is submitted with a value of 'cat', the value after binding will become 'at'.

Based on some searching I've done, I suspect it may have something to do with character encoding (which I have attempted to compare between environments), but I'm somewhat at a loss.

I can provide some code if it helps, though since the issue is only occurring on one server and not another, I'm not sure which (if any) of the symfony code will help.

Does any of this stand out as a rookie encoding oversight or something like that?

Edit: This happens with any number of leading 'c's, so 'cat' and 'ccat' and 'Ccccccat' will all be converted to 'at'

Edit2: I'm able to manually set the text field from the post variable after bind as a workaround( $document->setName($postvars['name']) ). It becomes more of an issue with the 'Reportbuilder' form, which has a variable number of nested forms (report has one or more tabs, tabs have one or more extra columns, etc) - so a similar workaround is clunky and not ideal

Edit3: Adding code

class DefaultController extends Controller
{

    public function indexAction()
    {
        ...
        $document = new Document();

        $form   = $this->createForm(new DocumentType($em,$user), $document);

        /// Here the name variable in the request is 'cat', as expected
        $form->bind($this->getRequest());
        /// Here the value of the 'name' parameter in the form is 'at'

        ...

    }
}

document.orm.yml:

Finance\GstBundle\Entity\Document:
    type:  entity
    manyToOne:
      report:
        targetEntity: Report
        mappedBy: report
      user:
        targetEntity: Finance\UserBundle\Entity\User
        mappedBy: user
    oneToMany:
      doctabs:
        targetEntity: Doctab
        mappedBy: document
      tabgroups:
        targetEntity: Tabgroup
        mappedBy: document
    table: document
    fields:
      id:
        type: integer
        id: true
        generator:
          strategy: AUTO
      name:
        type: string
        length: 255
        nullable: true
      path:
        type: string
        length: 255

... and the DocumentType definition:

namespace Finance\GstBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DocumentType extends AbstractType
{
    protected $em;
    protected $user;

public function __construct($em=null,$user=null){
    $this->em = $em;
    $this->user = $user;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{



        $builder
            ->add('report','entity',array(
                'class' => 'FinanceGstBundle:Report',
                'property'=>'name',
                'empty_value' => '--Choose a Template--'
            ))
            ->add('name')
            ->add('file')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Finance\GstBundle\Entity\Document'
        ));
    }

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

Upvotes: 3

Views: 394

Answers (2)

andrianoid
andrianoid

Reputation: 51

After patching RHEL on all 3 (dev/pre/prod) boxes, the issue has resolved itself. I'm doing a post-mortem to attempt to identify the specific package that caused the bug we were experiencing.

Our versions of RHEL were in different 'states' on the different servers. PRE/PROD were signifigantly further behind in system patches than Dev.

Upvotes: 2

Mick
Mick

Reputation: 31919

Problem: where 'cat' becomes 'at'?

1 Disable javascript to make sure it doesn't update your name value on submit.

2 Make sure the value submitted is 'cat'

$form->get('name')->getData(); //should return 'cat'

Note: it's mentioned you've done this in your post but we don't see the code.

3 You might have a listener somewhere that reacts to POST_BIND:

$builder->addEventListener(FormEvents::POST_BIND, $yourListener);

4 Maybe your using a data transformer, and there is something wrong with the way you transform() and reverseTransform(). A good way to debug would be to check your ViewModel in the NormModel and ModelData.

4 Maybe you have a lifecycle callback

/** @PrePersist */
public function doStuffOnPrePersist()
{
    $this->name = 'changed from prePersist callback!';
}

Upvotes: 0

Related Questions