How to remove validation lastname from prestashop 1.7.8.3 backoffice

Viewed 220
4 Answers

Finally, I have caught the issue. You are editing in admin panel and I was sharing code for front end. Please try below steps for admin:

Step 1 - file classes/Address.php

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

Change this to isAnything

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

Change your code to below code:

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]
                ),
            ]),
        ],
    ])

Now, you are ready to go and check.

Go to the file classes/Address.php file:

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

to :

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

validate to isAnything.

I think you were modifying in customer class. Please try with Address.php.

Thanks for sharing the files. I have resolved the case. You need to modify the classes/form/CustomerAddressForm.php

line 229

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

Change to:

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

I want to do this good with override. I have an issue with override this class. I have created module to override but it is not working. There is a way to override this without editing core files?

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

There is a file class override: \modules\pd_overridemodule\src\PrestaShopBundle\Form\Admin\Sell\Address\CustomCustomerAddressType.php

https://ibb.co/7QPHrqx

And I have an error when I am inside Edit Address Form Backoffice

Type error: Too few arguments to function PrestaShopBundle\Form\Admin\Sell\Address\CustomerAddressType::__construct(), 0 passed in C:\laragon\www\prestabiolab\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php on line 92 and exactly 5 expected

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

https://ibb.co/YfwhtKq


I have found a solution

Need to create module and call 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',
                        ),
                    ]),
                ],
            ]);
        }
    }
}

Now I think it works okey with override :)

Related