如何从 prestashop 1.7.8.3 后台删除验证姓氏

How to remove validation lastname from prestashop 1.7.8.3 backoffice

我有一个问题,如何在客户端地址编辑中删除 LastName 的验证。我需要在此字段中允许数字。

我在这里找到了线程 ,但是这个解决方案不起作用。

转到文件 classes/Address.php 文件:

'lastname' =>array('type' => self::TYPE_STRING, 'validate' => 'isCustomerName', 'required' => true, 'size' => 32),

至:

'lastname' =>array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required' => true, 'size' => 32),

验证为 isAnything

我认为您在客户 class 中进行了修改。请尝试 Address.php.

感谢您分享文件。 我已经解决了这个问题。您需要修改 classes/form/CustomerAddressForm.php

第 229 行

$isValid &= $this->validateField('lastname', 'isName', $this->translator->trans(
            'Invalid name',
            [],
            'Shop.Forms.Errors'
        ));

更改为:

$isValid &= $this->validateField('lastname', 'isAnything', $this->translator->trans(
        'Invalid name',
        [],
        'Shop.Forms.Errors'
    ));

终于找到问题了。您正在管理面板中编辑,而我正在共享前端代码。请管理员尝试以下步骤:

第 1 步 - 文件 classes/Address.php

'lastname' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required' => true, 'size' => 255],

将其更改为 isAnything

第 2 步 - src\PrestaShopBundle\Form\Admin\Sell\Address/CustomerAddressType.php

将您的代码更改为以下代码:

line 209: add('last_name', TextType::class, [
        'label' => $this->trans('Last name', 'Admin.Global'),
        'help' => $genericInvalidCharsMessage,
        'required' => true,
        'constraints' => [
            new NotBlank([
                'message' => $this->trans(
                    'This field cannot be empty.', 'Admin.Notifications.Error'
                ),
            ]),
            new CleanHtml(),
            new TypedRegex([
                'type' => TypedRegex::TYPE_GENERIC_NAME,
            ]),
            new Length([
                'max' => AddressConstraint::MAX_LAST_NAME_LENGTH,
                'maxMessage' => $this->trans(
                    'This field cannot be longer than %limit% characters',
                    'Admin.Notifications.Error',
                    ['%limit%' => AddressConstraint::MAX_LAST_NAME_LENGTH]
                ),
            ]),
        ],
    ])

现在,您可以去检查了。

我想用覆盖来做这件事。我对重写此 class 有疑问。我已经创建了要覆盖的模块,但它不起作用。有一种方法可以在不编辑核心文件的情况下覆盖它吗?

services:
    _defaults:
        public: true

    form.type.customer_address:
        class: 'Playdev\PrestaShopBundle\Form\Admin\Sell\Address\CustomCustomerAddressType'
        public: true
        arguments:
            - '@prestashop.adapter.form.choice_provider.country_state_by_id'
            - '@=service("prestashop.adapter.legacy.context").getContext().country.id'
            - '@router'
        tags:
            - { name: form.type }

https://ibb.co/VVjnJYr

有一个文件class覆盖: \modules\pd_overridemodule\src\PrestaShopBundle\Form\Admin\Sell\Address\CustomCustomerAddressType.php

https://ibb.co/7QPHrqx

当我在 Edit Address Form Backoffice 中时出现错误

类型错误:函数 PrestaShopBundle\Form\Admin\Sell\Address\CustomerAddressType::__construct() 的参数太少,在第 92 行的 C:\laragon\www\prestabiolab\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php 中传递了 0 个,恰好是第 5 个预计

[Symfony\Component\Debug\Exception\FatalThrowableError 0]

https://ibb.co/YfwhtKq


我找到了解决方法

需要创建模块并调用 hookactionCustomerAddressFormBuilderModifier。

public function hookactionCustomerAddressFormBuilderModifier(array $params)
{
    /** @var $formBuilder \Symfony\Component\Form */
    $formBuilder = $params['form_builder'];

    // remove lastname field
    $formBuilder->remove('last_name');

    // get all fields without removed
    $allFields = $formBuilder->all();

    // remove all fields
    foreach ($allFields as $inputField => $input) {
        $formBuilder->remove($inputField);
    }

    foreach ($allFields as $inputField => $input) {

        // normally add fields
        $formBuilder->add($input);

        // add fields after firstname
        if ($inputField == 'first_name') {
            $formBuilder->add('last_name', TextType::class, [
                'label' => $this->trans('Last name', [], 'Admin.Global'),
                'help' => $this->trans(
                    'Invalid characters:',
                    [], 
                    'Admin.Notifications.Info'
                ) . ' ' . TypedRegexValidator::GENERIC_NAME_CHARS,
                'required' => true,
                'constraints' => [
                    new NotBlank([
                        'message' => $this->trans(
                            'This field cannot be empty.', [], 'Admin.Notifications.Error'
                        ),
                    ]),
                    new CleanHtml(),
                    new TypedRegex([
                        'type' => TypedRegex::TYPE_GENERIC_NAME,
                    ]),
                    new Length([
                        'max' => AddressConstraint::MAX_LAST_NAME_LENGTH,
                        'maxMessage' => $this->trans(
                            'This field cannot be longer than %limit% characters',
                            ['%limit%' => AddressConstraint::MAX_LAST_NAME_LENGTH],
                            'Admin.Notifications.Error',
                        ),
                    ]),
                ],
            ]);
        }
    }
}

现在我认为它可以正常使用 override :)