Jump to content
soy.amarillo

Improvement of ContactController

Recommended Posts

Hello everybody.

 

I'm new in Prestashop. I started using it a week ago. While developing a catpcha validation for account form and contact form (link), I realized that ContactController could be improved by changing some lines.

 

The problem:

It happens when overriding the Class /controllers/ContactController for adding some extra validation.

You could think that overriding the method preProcess, adding the new validations and adding the errors to the $this->errors array will solve the problem, but it will send the form even if you do that.

 

The cause:

The way that this controller validates is not effective for overriding. This is the way it does:

 

if (!($from = trim(Tools::getValue('from'))) OR !Validate::isEmail($from))
			$this->errors[] = Tools::displayError('Invalid e-mail address');
		elseif (!($message = nl2br2($message)))
			$this->errors[] = Tools::displayError('Message cannot be blank');
		elseif (!Validate::isCleanHtml($message))
			$this->errors[] = Tools::displayError('Invalid message');
		elseif (!($id_contact = (int)(Tools::getValue('id_contact'))) OR !(Validate::isLoadedObject($contact = new Contact((int)($id_contact), (int)(self::$cookie->id_lang)))))
			$this->errors[] = Tools::displayError('Please select a subject on the list.');
		elseif (!empty($_FILES['fileUpload']['name']) AND $_FILES['fileUpload']['error'] != 0)
			$this->errors[] = Tools::displayError('An error occurred during the file upload');
		elseif (!empty($_FILES['fileUpload']['name']) AND !in_array(substr($_FILES['fileUpload']['name'], -4), $extension) AND !in_array(substr($_FILES['fileUpload']['name'], -5), $extension))
			$this->errors[] = Tools::displayError('Bad file extension');
		else
		{
	   ... //All checks are ok zone

 

Looking at this code, imagine that you override this method. Even if you add the error to the errors array, it will continue, as this method does not check the lenght of the array, just check the default fields and continue.

 

The proposed solution:

 

The solution to this is not difficult. Just a quick view of another controller, eg AuthController, give us a clue of how to solve this. A simple check of the size of the array will help daughter classes to check custom fields added to the form.

Example:

 

if (!($from = trim(Tools::getValue('from'))) OR !Validate::isEmail($from))
			$this->errors[] = Tools::displayError('Invalid e-mail address');
		if (!($message = nl2br2($message)))
			$this->errors[] = Tools::displayError('Message cannot be blank');
		if (!Validate::isCleanHtml($message))
			$this->errors[] = Tools::displayError('Invalid message');
		if (!($id_contact = (int)(Tools::getValue('id_contact'))) OR !(Validate::isLoadedObject($contact = new Contact((int)($id_contact), (int)(self::$cookie->id_lang)))))
			$this->errors[] = Tools::displayError('Please select a subject on the list.');
		if (!empty($_FILES['fileUpload']['name']) AND $_FILES['fileUpload']['error'] != 0)
			$this->errors[] = Tools::displayError('An error occurred during the file upload');
		if (!empty($_FILES['fileUpload']['name']) AND !in_array(substr($_FILES['fileUpload']['name'], -4), $extension) AND !in_array(substr($_FILES['fileUpload']['name'], -5), $extension))
			$this->errors[] = Tools::displayError('Bad file extension');

		if (sizeof($this->errors) === 0) {

 

Look as I deleted the elseif of the second and other field checks. In this way, the user will see all the errors in the contact form page, even the custom ones.

 

Hope this help. Reviews are welcome.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More