问题
how can in Magento remove mandatory required for the review fields (nickname, summary of your review and review). I found one answer but it is just for admin panel: Magento Admin Add/Edit Review -> removing Summary of Review Field required validation Would like to remove it in store view, so that customers can just click on rating and send it.
Thx
回答1:
(I'm using Magento2)
Two places code must be changed:
/var/www/magento2/app/code/Magento/Review/Model/Review.php
As soon as you open this file, save yourself some time and hit ctrl+F and type the phrase validate. Should take you here...
public function validate()
{
$errors = [];
// if (!\Zend_Validate::is($this->getTitle(), 'NotEmpty')) {
// $errors[] = __('Please enter a review summary.');
// }
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;
}
You may notice that 2 of the 3 if statements are commented out. I did this in order to remove the requirement for only summary and review. I wanted to keep Nickname as a required field so that isn't commented out.
Next you need to copy this file from the default Magento Review directory into your local theme directory...
/var/www/magento2/app/code/Magento/Review/view/frontend/templates/form.phtml
Make sure to get the form.phtml from the Review module. There are several other form.phtml files in other modules as well.
An example of where to copy the file to:
/var/www/magento2/app/design/frontend/Danny/orange/Magento_Review/templates/form.phtml
Once you have copied this into your own local directory, you can proceed to make the changes :
<div class="field review-field-nickname required">
<label for="nickname_field" class="label"><span><?php echo $block->escapeHtml(__('Nickname')) ?></span></label>
<div class="control">
<input type="text" name="nickname" id="nickname_field" class="input-text" data-validate="{required:true}" data-bind="value: nickname()" />
</div>
</div>
<div class="field review-field-summary">
<label for="summary_field" class="label"><span><?php echo $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>
<div class="field review-field-text">
<label for="review_field" class="label"><span><?php echo $block->escapeHtml(__('Review')) ?></span></label>
<div class="control">
<textarea name="detail" id="review_field" cols="5" rows="3" data-validate="{required:false}" data-bind="value: review().detail"></textarea>
</div>
</div>
In the above code snippet you will notice that the first block "Nickname" has been left as-is. The requirement is kept there.
The other two boxes have been modified to remove the requirement. The only changes I had to make were to remove the "required" class and change data-validate="{required:false}" from true to false.
Hope this helps!
回答2:
Simply go to:
app >> design >> frontend >> {{theme package}} >> {{theme}} >> template >> review >> form.phtml
and remove required-entry from class from which fields you want to mandatory fields.
来源:https://stackoverflow.com/questions/26066741/how-can-i-remove-mandatory-required-for-the-review-fields-nickname-summary-of