Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Product reviews: broken validation when updating as admin #9895

@palttamas

Description

@palttamas

Sylius version affected: 1.2.3

Description
Product reviews cannot be updated in the administrator screen. User faces a "This email is already registered, please login or use forgotten password." validation error message.

Steps to reproduce

  1. Go to https://demo.sylius.com/admin/product-reviews/
  2. Click "Edit" on any review
  3. Click "Save Changes"

Possible Solution
My gut feeling is that this is happening because the shop validation is being applied, although I have not looked into it that much.

At the moment, I fixed the issue by overriding the resource configuration, created a ProductReview form type that extends ReviewType, then swapped out the default form template to my custom one.

My use-case also involved having to add the ability for admins to create reviews (thus the except: ['show'] part), so this workaround did the trick:

Resource config:

sylius_admin_product_review:
  resource: |
    alias: sylius.product_review
    section: admin
    permission: true
    templates: SyliusAdminBundle:Crud
    except: ['show']
    redirect: update
    grid: sylius_admin_product_review
    form: AppBundle\Form\Type\ProductReview
    vars:
      all:
        subheader: sylius.ui.manage_reviews_of_your_products
        templates:
          form: AppBundle:ProductReview:_form.html.twig
      index:
          icon: newspaper
      create:
        header: "Add product review"
        subheader: "Creates a new product review"
        icon: 'add'
        templates:
          form: AppBundle:ProductReview:_form.html.twig
  type: sylius.resource

Custom ProductReview form type

<?php

namespace AppBundle\Form\Type;


use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormBuilderInterface;
use Sylius\Component\Review\Model\ReviewInterface;
use Sylius\Bundle\ReviewBundle\Form\Type\ReviewType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Required;
use Sylius\Bundle\ProductBundle\Form\Type\ProductAutocompleteChoiceType;

class ProductReview extends ReviewType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        parent::buildForm($builder, $options);

        $builder->add('author');

        $builder->add('reviewSubject', ProductAutocompleteChoiceType::class, [
            'label' => 'Product',
            'required' => true,
            'constraints' => [new NotBlank(), new Required()]
        ]);

        $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
            $form = $event->getForm();

            /** @var ReviewInterface $review */
            $review = $event->getData();

            if(!$review->getReviewSubject()) {
                $form->get('reviewSubject')->addError(new FormError('Product is required'));
            }

            if(!$review->getAuthor()) {
                $form->get('author')->addError(new FormError('Review author is required'));
            }
        });
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix(): string
    {
        return 'sylius_product_review';
    }
}

Custom form twig

<div class="ui stackable grid">
    <div class="twelve wide column">
        <div class="ui segment">
            {{ form_errors(form) }}
            {{ form_row(form.title) }}
            {{ form_row(form.comment) }}
            {{ form_row(form.rating) }}
            {{ form_row(form.author) }}
            {{ form_row(form.reviewSubject) }}
        </div>
    </div>
    <div class="four wide column">
        {% if product_review.reviewSubject %}
            {% include '@SyliusAdmin/ProductReview/_product.html.twig' %}
        {% endif %}
        {% if product_review.author %}
            {% include '@SyliusAdmin/ProductReview/_author.html.twig' %}
        {% endif %}
    </div>
</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Potential BugPotential bugs or bugfixes, that needs to be reproduced.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions