Rúben Martins Posted July 11, 2023 Share Posted July 11, 2023 Hello, Using the default contact form it only requires the subject(dropdown), email and message as mandatory by default, how can I set the remaining fields as mandatory like name and phone number? Tried this: but didn't help. Link to comment Share on other sites More sharing options...
AddWeb Solution Posted July 12, 2023 Share Posted July 12, 2023 Hi, You might need to override the contact.php file which is responsible for the contact form field settings. 'required' => true Add this attribute to the field settings which needs to be made as a mandatory field. Clear the cache and check If this works. Link to comment Share on other sites More sharing options...
Rúben Martins Posted July 12, 2023 Author Share Posted July 12, 2023 Hi @AddWeb Solution In witch path can I find this file? Can you provide an example for the phone number and Name as required? Link to comment Share on other sites More sharing options...
ComGrafPL Posted July 12, 2023 Share Posted July 12, 2023 Have you checked in Customers > Addresses > Set required fields for this section Link to comment Share on other sites More sharing options...
Rúben Martins Posted July 12, 2023 Author Share Posted July 12, 2023 Thank you @ComGrafPL I believe that only applies to the checkout process, not a direct contact trough the contact form in the contact us page. Link to comment Share on other sites More sharing options...
AddWeb Solution Posted July 13, 2023 Share Posted July 13, 2023 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 More sharing options...
Rúben Martins Posted July 17, 2023 Author Share Posted July 17, 2023 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 More sharing options...
AddWeb Solution Posted July 18, 2023 Share Posted July 18, 2023 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now