3

I have been trying to figure this out for over an hour and I have made no progress...

I have the following form and its the only field:

<?php
$builder
        ->add('cars', 'collection', array(
            'type' => new CarType(),
            //'property_path' => '[cars]',
            'required' => false,
        ))
    ;

This form receives an array, with one key "cars" pointing to an array of Car objects (which are handled fine by the CarType().

When I set data to this form, it gives me a Property Path parse error, stating that the property path "[]" is invalid and cant be parsed. Note that if I uncomment the property_path option above it is the same error (I just put that there to show its the default).

  1. Why is it using a property path of []?
  2. How can I fix this?

NOTE: This works fine in one enviornment with PHP 5.4 and fails on all my PHP 5.3 environments.

Thanks for any help.

More information:

Exception: "Could not parse property path "[]". Unexpected token "[" at position 0"

in /vagrant/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyPath.php at line 125

1
  • please add the entity holding the cars , property, getter/setter to the question. aswell as your CarType form Commented Jun 26, 2013 at 20:00

2 Answers 2

3

I had a similar problem, and found this workaround, which I'll adapt to your example:

I was using the form to create some new Car objects, so my cars didn't have IDs. My Car->getId() method returned null. When I changed the getId() method to this:

public function getId() {
    return (int) $this->id;
}

the problem went away, because now getId() is returning 0. Presumably the property path [0] is valid, but [] is not.

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

Comments

1

Your return value of the getCars() should normally not be of type array but of type Doctrine\Common\Collections\ArrayCollection.

property_path specifies the property which shall be used by the form to get/set the values back to the underlying object.

if your field-name is 'cars' it will already default to 'cars'.

Please note that you don't have to wrap the property_path's value in [] ...


PHP 5.3 does not support the short array syntax [] which has been added in PHP 5.4.

For backward compatibility you have to use array() syntax.

4 Comments

I don't believe anywhere in my code I am using the [] array syntax. This seems to be an error thrown by an invalid "[]" property_path in the form component.
you don't have to wrap the property_path in [] ... and it defaults to the field-name ... you don't need it in your case
I have commented it out just to show that it will default to what is in the comment. I still get the same error with that commented out.
jeah i got that - but the commented out value includes [] aswell, which is wrong.

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.