Sylius: Shipping Error when revisiting shipping method page

Created on 8 Jul 2015  Â·  21Comments  Â·  Source: Sylius/Sylius

Here is the error:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Proxies__CG__\Sylius\Component\Core\Model\ShippingMethod. You can avoid this error by setting the "data_class" option to "Proxies__CG__\Sylius\Component\Core\Model\ShippingMethod" or by adding a view transformer that transforms an instance of class Proxies__CG__\Sylius\Component\Core\Model\ShippingMethod to scalar, array or an instance of \ArrayAccess.

How to re-create the error is as follows:

Go through the checkout as usual, however when you get to the payment details page (last page), click on the basket in the header (back to /cart) and go through the checkout steps again.
Once you hit the shipping method page you will see the error above.

This happens currently on my local (v0.14) and on the current live demo.

Potential Bug

Most helpful comment

@songecko if you need a quick fix for the checkout you can override ShippingMethodChoiceType and set data_class to null (take a look at the gist), but @NeverResponse is working on a proper fix, so it should be resolved soon.

All 21 comments

@carlowens Can you supply some more details? The content of the cart, etc...? I've just tested this with a fresh install of 6c7d32d, and I can visit the page as much as I want without errors.

I only found this issue when updating from v0.11 to v0.14 which we have now decided to wait as there are a couple of issues which we don't have time to iron out right now.

But as I said I can get this error on the Sylius demo site (http://demo.sylius.org/).

Simply by adding a product to the card (any as far as i can tell), proceeding to the payment page (last page) and then clicking back on the basket and going through the steps again. Once you reach the shipping method page you get the error.

Sorry I don't have time at the moment to provide more info or a behat example.

Running into the same thing after an upgrade to 0.14

I tracked this down to an issue with the ShippingMethodChoiceType in the ShippingBundle. It works perfectly when a shipping method isn't selected, but breaks when a method is selected for the order.

This may have something to do with upgrading symfony versions. The fix is to simply add a data_class to the OptionsResolver defaults.

Also - as a workaround before 0.15 is released

namespace AppBundle\Form;

use Sylius\Bundle\CoreBundle\Form\Type\Shipping\ShippingMethodChoiceType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ShippingMethodChoiceType extends BaseType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    {
        parent::setDefaultOptions($resolver);
        $resolver->setDefaults(array(
            'data_class' => 'Sylius\Component\Shipping\Model\ShippingMethodInterface',
        ));
    }
}

And in services.yml

parameters:
  sylius.form.type.shipping_method_choice.class: AppBundle\Form\ShippingMethodChoiceType

When the form type is set to use "multiple" my fix didn't work. I updated the PR with a different solution - to add a transformer for when "multiple" is not selected.

Not sure it's the best possible solution, but I didn't have time to dig further.

Thanks for this tip !

I'm in 0.14, previously the ShippingMethodChoiceType was in Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodChoiceType.

So for 0.14:

namespace AppBundle\Form\Type;

use Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodChoiceType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ShippingMethodChoiceType extends BaseType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        parent::setDefaultOptions($resolver);

        $resolver->setDefaults(array(
            'data_class' => 'Sylius\Component\Shipping\Model\ShippingMethodInterface',
        ));
    }
}

@SebLours just so you know - that won't be a complete solution in the latest - check out the PR referenced against this: #3085 . I used a data transformer so that it would work both in the channels forms and the shipping page.

I'm not sure what the best way to fix it actually is but the data transformer approach was compatible with the other uses of the form.

Should be fixed after #3406.

@michalmarcinkowski @pjedrzejewski The error still happen on 0.18@dev:

"The form's view data is expected to be an instance of class Sylius\Component\Shipping\Model\ShippingMethodInterface, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Sylius\Component\Shipping\Model\ShippingMethodInterface."

Any solution?

Have you tried with the newest master? Should be fixed by #4702.

Yes, but sill happened. To reproduce this:

1- Go through the checkout as usual and select the shipping method.
2- On the Payment method step, go back to the cart again.
3- Go to checkout and when you submit the addressing form, the error happen.

Exception

@songecko tried to reproduce on current master and didn't manage to do it. Are you sure you're using the newest codebase? I'm pretty sure this shouldn't occur anymore.

@songecko The same thing happens to me, I select a shipping method hit next, then when I hit back I get that error.
My composer:

 "require": {
        "php": ">=5.5.9",

        "sylius/sylius": "0.18.*@dev",
        "sensio/generator-bundle": "^2.3",
        "sensio/distribution-bundle": "^3.0"
    },

@michalmarcinkowski @pjedrzejewski I think this is a bug to fix urgently. Also, you can reproduce the error on http://demo.sylius.org/

Actually the #4702 PR was wrong. As we can read in the symfony docs the transform method converts the model to normalized data and reverse transform converts the normalized data to the model. That being said, the changes from that PR should be reverted what should resolve this issue.

Thanks @michalmarcinkowski. Of that PR we need to revert the changes of buildForm method of the Sylius/Bundle/ShippingBundle/Form/Type/ShippingMethodChoiceType class. Sorry i dont have experience solving bugs, can you do that?

Sorry @michalmarcinkowski we need modify the ObjectToIdentifierTransformer. The weird thing is in both transform and reverseTransform method the parameter is a identifier and we need return a ShippingMethodInterface. This implementations works fine, i can pass all checkout steps and start again and no errors: https://gist.github.com/songecko/a840c2650a1ccc16236a0bb1f9d28d31.
Please, let me know your opinions.

@songecko if you need a quick fix for the checkout you can override ShippingMethodChoiceType and set data_class to null (take a look at the gist), but @NeverResponse is working on a proper fix, so it should be resolved soon.

after upgrade of 0.17 to 0.18, got this error and this is not a revisit, but first time access

Fixed in #4978.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eb22fbb4 picture eb22fbb4  Â·  3Comments

ping86 picture ping86  Â·  3Comments

mikemix picture mikemix  Â·  3Comments

reyostallenberg picture reyostallenberg  Â·  3Comments

javiereguiluz picture javiereguiluz  Â·  3Comments