Jump to content

[solved] Validate extra fields in contactform


Keron83

Recommended Posts

Hi Everyone.

 

I'm busy to add extra fields to the contactform. (Prestashop 1.5.6.2)

 

Thanks to this tutorial it's all (almost) working:

http://nemops.com/adding-new-fields-to-prestashop-contact-form/#.VWbyqkZcptE

 

I've added some extra fields that needs validation.

 

- I've created an override for: CustomerThread.php (just a the tutorial says) (see attachment)

I've added the following rules to CustomerThread with some validation:

            'name' =>  array('type' => self::TYPE_STRING, 'validate' => 'isName'),
            'address' =>  array('type' => self::TYPE_STRING, 'validate' => 'isAddress'),
            'postcode' =>  array('type' => self::TYPE_STRING, 'validate' => 'isPostCode'),
            'city' =>  array('type' => self::TYPE_STRING, 'validate' => 'isCityName'),
            'number' =>  array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber')

Everything is fine till one of the extra field  is not valid.  (example:hsfjdhsjsd as phonenumber -> is not valid)

Then I get this error:

Property CustomerThread->number is not valid
at line 837 in file classes/ObjectModel.php

831. 
832. 			$message = $this->validateField($field, $this->$field);
833. 			if ($message !== true)
834. 			{
835. 				if ($die)
836. 					throw new PrestaShopException($message);
837. 				return $error_return ? $message : false;
838. 			}
839. 		}
840. 
841. 		return true;

If the extra fields are filled in correctly everything works.

Can anyone help?

 

See also the files That I  attached.

CustomerThread.php

ContactController.php

contact-form.tpl.txt

message.tpl.txt

Edited by Keron83 (see edit history)
Link to comment
Share on other sites

I think you need to be more specific about your question

 

 

You set 

 

'number' => array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber')

 

 

You set number a String but validate like isPhoneNumber ( Just number ) then you get the error because you get a word

 

 You need set another type of data or you need validate the data

Link to comment
Share on other sites

Thanks.

 

I solved it by doing it this way:

				//check custom fields
						
					//Check Name			
						elseif (!($name = nl2br2(Tools::getValue('name'))))
							$this->errors[] = Tools::displayError('Field is required');
						elseif (!preg_match(Tools::cleanNonUnicodeSupport('/^[^0-9!<>,;?=+()@#"°{}_$%:]*$/u'), stripslashes($name)))
							$this->errors[] = Tools::displayError('Enter a valid name');	
					//Check Address	
						elseif (!($address = nl2br2(Tools::getValue('address'))))
							$this->errors[] = Tools::displayError('Field is required');
						elseif (!preg_match('/^[^!<>?=+@{}_$%]*$/u', $address))
							$this->errors[] = Tools::displayError('Enter a valid address');
					    
					//Check Postcode	
						elseif (!($postcode = nl2br2(Tools::getValue('postcode'))))
							$this->errors[] = Tools::displayError('Field is required');
						elseif (!preg_match('/^[a-zA-Z 0-9-]+$/', $postcode))
							$this->errors[] = Tools::displayError('Enter a valid postcode');							
					
					//Check City
						elseif (!($city = nl2br2(Tools::getValue('city'))))
							$this->errors[] = Tools::displayError('Field is required');
						elseif (!preg_match('/^[^!<>;?=+@#"°{}_$%]*$/u', $city))
							$this->errors[] = Tools::displayError('Enter a valid city');
					
					//Check Phonenumber
						elseif  (!($number = nl2br2(Tools::getValue('number'))))				
							$this->errors[] = Tools::displayError('Field is required');
						elseif (!preg_match('/^[+0-9. ()-]*$/', $number))
							$this->errors[] = Tools::displayError('Enter a valid phonenumber');

This solution works for me.

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...