3

I want delete filed Summary from reviews product form.

In file vendor/magento/module-review/view/frontend/templates/form.phtml

I comment line:

<div class="field review-field-summary required">
    <label for="summary_field" class="label"><span><?= $block->escapeHtml(__('Summary')) ?></span></label>
    <div class="control">
        <input type="text" name="title" id="summary_field" class="input-text" data-validate="{required:false}" data-bind="value: review().title" />
    </div>
</div>

Currently, the "Summary" field is not displayed. But when trying to add reviews, I get an error that I need to fill this field. How to solve it?

I try delete this filed from reviews form: https://prnt.sc/o2v4w0

I delete this in form.html and now not displayed: https://prnt.sc/o2v56q

But when I try add review: https://prnt.sc/o2v5kl Then error because system still required this field.

  • Can you take a screenhot of that error and add it your question? – Sukumar Gorai Jun 17 at 10:53
  • I edit my question – Sylvester Jun 17 at 10:59
1

You can achieve your requirement by creating a module with name STech_Review by following steps:

Step 1: Create Registration.php under

app/code/STech/Review/registration.php

with content

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'STech_Review',
    __DIR__
);

Step 2: Create module.xml under

app/code/STech/Review/etc/module.xml

with content

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="STech_Review" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Review"/>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Step 3: Create di.xml under

app/code/STech/Review/etc/di.xml

with content

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Review\Model\Review" type="STech\Review\Model\Review"/>
</config>

Step 4: Create Review.php under

app/code/STech/Review/Model/Review.php

with content

<?php
namespace STech\Review\Model;

class Review extends \Magento\Review\Model\Review
{
    public function validate()
    {
        $errors = [];

        if (!\Zend_Validate::is($this->getNickname(), 'NotEmpty')) {
            $errors[] = __('Please enter a nickname.');
        }

        if (!\Zend_Validate::is($this->getDetail(), 'NotEmpty')) {
            $errors[] = __('Please enter a review.');
        }

        if (empty($errors)) {
            return true;
        }
        return $errors;
    }
}

Finally you need to alter the table review_details by changing field title to optional(Nullable true).

Thats it. Run setup:upgrade, di:compile and other required commands and test.

  • Ok but this magento module is located at: vendor/magento/module-review/ not in app/code. – Sylvester Jun 17 at 11:12
  • Yes I am asking you to create own module and override in this way. – Sukumar Gorai Jun 17 at 11:12
  • Are you getting my point? – Sukumar Gorai Jun 17 at 11:22
  • I add this code in app/code after setup:upgrade and deploy no result. Still required this field – Sylvester Jun 17 at 11:26
  • Did you create complete module? Or only added these codes? – Sukumar Gorai Jun 17 at 11:26
0

Try to add a default value for summary in your controller file before saving review. This is mandatory field in review model. Else you can modify models to make it non-mandatory. Best way to set some default value so that it get saved but we can manage the output at frontend.

  • in which file controlller is located this file? – Sylvester Jun 17 at 11:06
0

Magento validates for the required fields of the review form in the /vendor/magento/module-review/Model/Review.php file.

First, you need to override this file. To do so, create a file app/code/Vendor/Module/etc/di.xml and write the following code in it:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Review\Model\Review" type="Vendor\Module\Model\Review"/>
</config>

Next, create the Model file app/code/Vendor/Module/Model/Review.php and put the following code in this file:

<?php
namespace Vendor\Module\Model;

class Review extends \Magento\Review\Model\Review
{
    public function validate()
    {
        $errors = [];

        if (!\Zend_Validate::is($this->getNickname(), 'NotEmpty')) {
            $errors[] = __('Please enter a nickname.');
        }

        if (!\Zend_Validate::is($this->getDetail(), 'NotEmpty')) {
            $errors[] = __('Please enter a review.');
        }

        if (empty($errors)) {
            return true;
        }
        return $errors;
    }
}

Note: I assume that you know how to create a basic module in Magento 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.