Jump to content

How to set remaining contact form fields as mandatory?


Rúben Martins

Recommended Posts

19 hours ago, Rúben Martins said:

Can you provide an example for the phone number and Name as required?

To add phone number field, Add the code something like below :

'phone' => [
  'type' => self::TYPE_STRING,
  'validate' => 'isPhoneNumber',
  'size' => 32,
  'required' => true,
],

Similarly, the 'name' field already has the 'required' => true attribute present in it.

Clear the cache and check If this works.

Link to comment
Share on other sites

Hi @AddWeb Solution thank you for the feedback,

I've added to the classes/Contact.php and cleared the cache but still does not work.

This is what I've, but with no effect

For reference I've Prestashop 1.7.8.8.

 

On another test I've commented the email field in the class and does not take any effect, so looks like this validation is done somewhere else? I've looked in the overide folder and theme and can't find anything that is overiding this.

	/**
     * @see ObjectModel::$definition
     */
    public static $definition = [
        'table' => 'contact',
        'primary' => 'id_contact',
        'multilang' => true,
        'fields' => [
            'email' => [
                'type' => self::TYPE_STRING,
                'validate' => 'isEmail',
                'size' => 255,
            ],
            'customer_service' => [
                'type' => self::TYPE_BOOL,
                'validate' => 'isBool',
            ],

            /* Lang fields */
            'name' => [
                'type' => self::TYPE_STRING,
                'lang' => true,
                'validate' => 'isName',
                'required' => true,
                'size' => 255,
            ],
            'phone' => [
                'type' => self::TYPE_STRING,
                'validate' => 'isPhoneNumber',
                'size' => 12,
                'required' => true,
            ],
            'message' => [
                'type' => self::TYPE_STRING,
                'validate' => 'isCleanHTML',
                'size' => 50,
                'required' => true,
            ],
        ],
    ];

 

Link to comment
Share on other sites

Hi,

Try creating a file override/controllers/front/ContactController.php and define the overridden class as follows:

<?php

class ContactController extends ContactControllerCore
{
    public function postProcess()
    {
        // Validate the form fields
        if (Tools::isSubmit('submitMessage')) {
            $this->validateContactForm();
        }

        parent::postProcess();
    }

    private function validateContactForm()
    {
        $requiredFields = array('name', 'email', 'phone', 'message');
        foreach ($requiredFields as $field) {
            if (empty(Tools::getValue($field))) {
                $this->errors[] = $this->trans('The %field% field is required.', array('%field%' => $field), 'Shop.Notifications.Error');
            }
        }
    }
}

After making this changes, clear the cache and test If it works or atleast reflects the changes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...