<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NemoPS &#187; validation</title>
	<atom:link href="http://nemops.com/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>Forbid the usage of junk-alias emails in Prestashop</title>
		<link>http://nemops.com/validate-junk-addresses-prestashop/</link>
		<comments>http://nemops.com/validate-junk-addresses-prestashop/#comments</comments>
		<pubDate>Wed, 12 Mar 2014 10:17:55 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[prestashop]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1905</guid>
		<description><![CDATA[<p>If you want your customers to use a valid email address, you might want to forbid the use of expiring aliases. Let&#8217;s create a module to handle this validation! Version used: Prestashop 1.5.6.2 Compatibility: Prestashop 1.5, Prestashop 1.6 Introduction Depending on your market, a certain percentage of your clients might be using junk email aliases [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/validate-junk-addresses-prestashop/">Forbid the usage of junk-alias emails in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>If you want your customers to use a valid email address, you might want to forbid the use of expiring aliases. Let&#8217;s create a module to handle this validation!</p>
<p><span id="more-1905"></span></p>
<a class="download-files button style1" href="http://nemops.com/wp-content/uploads/2014/03/emailchecker_tut.zip" title="Download Project Files">Download Project Files</a>
<ul>
<li>Version used: <strong>Prestashop 1.5.6.2</strong></li>
<li>Compatibility: Prestashop 1.5, Prestashop 1.6</li>
</ul>
<h2>Introduction</h2>
<p>Depending on your market, a certain percentage of your clients might be using junk email aliases when registering or checking out as guests on your site. If you strive to keep a solid customer base, and especially if you want to fidelize them, this might become a real issue at a certain point. Therefore, you want to make sure they use at least a valid, non-junk/alias address on your site.</p>
<p>The issue can easily be solved with the use of <strong>action Hooks</strong>. Prestashop handles address validation through the so-called &#8220;AuthController&#8221;, and wisely comes with an action hook right at the beginning of the  method used to process a new account (be it guest or not) creation.</p>
<p>In today&#8217;s tutorial, we will create a small, lightweight module that hooks to <strong>actionBeforeSubmitAccount</strong>, so we can target any keyword we dislike, and prevent its usage within the email address field. Let&#8217;s get to it!</p>
<div class="separator"></div>
<h2>Step 1 &#8211; Creating the Prestashop Module and the back office interface</h2>
<p>In order to make things easier for us, we will create a back office interface that saves all these words in a single database configuration entry. This way, whenever we feel like adding a new &#8220;banner keyword&#8221; for the email address field, we can simply reach out the module&#8217;s configuration page, add it and save.</p>
<p>Let&#8217;s start by adding a new, simple module. Download the project files, grab the <strong>emailchecker</strong> folder that you find inside the &#8216;start_here&#8217; folder, and drop it inside your <em>modules/</em> folder.</p>
<p>Open up <strong>emailchecker.php</strong>, then modify the <strong>install()</strong> method by registering the new hook, as follows:</p>
<pre class="brush: php; title: ; notranslate">
	public function install()
	{
		if (!parent::install() OR			
			!$this-&gt;registerHook('actionBeforeSubmitAccount'))
			return false;
		return true;
	}
</pre>
<p><strong>actionBeforeSubmitAccount</strong> is the name of the hook we want to target.</p>
<p>Then, let&#8217;s create the markup to handle banned keywords from the back office. Inside the <strong>_displayForm()</strong> method, add the following:</p>
<pre class="brush: php; title: ; notranslate">
		$this-&gt;_html .= '
			&lt;form action=&quot;'.$_SERVER['REQUEST_URI'].'&quot; method=&quot;post&quot;&gt;
				&lt;fieldset&gt;&lt;legend&gt;&lt;img src=&quot;'.$this-&gt;_path.'logo.gif&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;'.$this-&gt;l('Settings').'&lt;/legend&gt;
		';

		$this-&gt;_html .='
					&lt;label&gt;'.$this-&gt;l('Banned keywords').'&lt;/label&gt;
					&lt;div class=&quot;margin-form&quot;&gt;
						&lt;input type=&quot;text&quot; name=&quot;bannedkw&quot; value=&quot;'.Configuration::get('PS_EMAIL_BANNED_KEYWORDS').'&quot;&gt;
					&lt;/div&gt;
					
		';

		/* Submit button */
		$this-&gt;_html .='&lt;p class=&quot;center&quot;&gt;&lt;input type=&quot;submit&quot; name=&quot;submitKw&quot; value=&quot;'.$this-&gt;l('Save Settings').'&quot;&quot; class=&quot;button&quot;&gt;&lt;/p&gt;';


		$this-&gt;_html .= '
				&lt;/fieldset&gt;
			&lt;/form&gt;';
</pre>
<p>It&#8217;s a regular, simple form. If you are not used to creating Prestashop Modules, you might want to have a look at <a href="http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module" title="how to create a prestashop module" rel="nofollow">how to create a prestashop module </a> in the official Prestashop Documentation.</p>
<p>Briefly: we added a new field to hold the banned keywords. We will add them in the input one by one, separated by comma. Then, we will save them inside the <strong>PS_EMAIL_BANNED_KEYWORDS</strong>, and this very same value will be displayed, if set, each time we access the form.</p>
<p>At this point, let&#8217;s save the entries to the configuration table. Edit the <strong>_postProcess()</strong> method as follows:</p>
<pre class="brush: php; title: ; notranslate">
	private function _postProcess()
	{
		if (Tools::isSubmit('submitKw')) // handles the basic config update
		{
			Configuration::updateValue('PS_EMAIL_BANNED_KEYWORDS', Tools::getValue('bannedkw'));
			$this-&gt;_html .= $this-&gt;displayConfirmation($this-&gt;l('Keywords updated'));
		}
	}

</pre>
<p>We don&#8217;t really need validation, but feel free to add it if you want to make sure data is entered correctly here (words separated by commas, so Regular Expressions will come in handy).</p>
<p>As a last thing, if you want to make sure no trace is left into the database upon the module&#8217;s removal, erase the configuration entry by editing the uninstall method:</p>
<pre class="brush: php; title: ; notranslate">
public function uninstall()
	{
		if (!parent::uninstall() OR !Configuration::deleteByName('PS_EMAIL_BANNED_KEYWORDS'))
			return false;
		return true;
	}
</pre>
<p>We are done with the back office! Time to install the module. Reach out the modules panel in the back office, look for <strong>emailchecker</strong> and install it. Test the configuration page and make sure you can save keywords properly. Once this is settled, move to the next step!</p>
<div class="separator"></div>
<h2>Validating the email address</h2>
<p>It&#8217;s time to create the hook method that will handle email validation. Inside our module&#8217;s php file, create a new method named <strong>hookActionBeforeSubmitAccount</strong>:</p>
<pre class="brush: php; title: ; notranslate">
	public function hookActionBeforeSubmitAccount($params)
	{
	}
</pre>
<p>Let&#8217;s first grab our keywords:</p>
<pre class="brush: php; title: ; notranslate">
	public function hookActionBeforeSubmitAccount($params)
	{
		
		$banned_keywords = Configuration::get('PS_EMAIL_BANNED_KEYWORDS');
		if(!$banned_keywords)
			return;

	}

</pre>
<p>If nothing is found for that configuration entry, return, as there is nothing to do. Otherwise, explode the keywords string using the comma separator, and iterate through them using a foreach:</p>
<pre class="brush: php; title: ; notranslate">

	public function hookActionBeforeSubmitAccount($params)
	{
		
		$banned_keywords = Configuration::get('PS_EMAIL_BANNED_KEYWORDS');
		if(!$banned_keywords)
			return;
		// explode keywords by comma
		$kw = explode(',', $banned_keywords);
		foreach ($kw as $k) {
			
		}

		return;
	}

</pre>
<p>Finally, let&#8217;s check the presence of our banned keyword inside the email address. There are several methods to do this. The easiest one is using the <strong>strstr</strong> function of PHP. Of course, you might as well use regular expressions if you feel more comfortable with it:</p>
<pre class="brush: php; title: ; notranslate">

	public function hookActionBeforeSubmitAccount($params)
	{
		
		$banned_keywords = Configuration::get('PS_EMAIL_BANNED_KEYWORDS');
		if(!$banned_keywords)
			return;
		// explode keywords by comma
		$kw = explode(',', $banned_keywords);
		foreach ($kw as $k) {
			if (strstr(strtolower(Tools::getValue('email')), strtolower($k))) {
				$this-&gt;context-&gt;controller-&gt;errors[] = Tools::displayError('Invalid email address');
			}
		}

		return;
	}

</pre>
<p>We are telling the script: if the keyword is found inside the email address (if it&#8217;s a substring of it, grabbed using the helper getValue, which retrieves $_POST and $_GET data) add and error to the current controller. Since the current controller is <strong>authController</strong>, the information validation will not proceed, and nothing will be added to the database. This happens because Prestashop checks the size of the <strong>errors</strong> array right before adding data to the store.</p>
<p>Save and test everything out. If it works properly, you should see the message popping out as soon as you try to register using an address containing the banner keyword. This will work for both the standalone account creation, registered client checkout, and guest checkout (both 5-steps and one page).</p>
<p><a href="http://nemops.com/wp-content/uploads/2014/03/email_checker.png"><img src="http://nemops.com/wp-content/uploads/2014/03/email_checker-680x472.png" alt="Forbid junk-alias email usage in Prestashop" width="680" height="472" class="aligncenter size-large wp-image-1904" /></a></p>
<div class="separator"></div>
<h2>Conclusion</h2>
<p>Validating the email address is a very limited usage of the ActionBeforeSubmitAccounthook. You can validate all data sent via $_POST using the account creation form with it. Here is the complete array, take from Prestashop 1.5.6.2:</p>
<pre>
'id_gender' => string '1' (length=1)
  'customer_firstname' => string 'Fabio' (length=6)
  'customer_lastname' => string 'Porta' (length=6)
  'email' => string 'something@mailexpire.com' (length=23)
  'passwd' => string 'mypassw' (length=7)
  'days' => string '' (length=0)
  'months' => string '' (length=0)
  'years' => string '' (length=0)
  'referralprogram' => string '' (length=0)
  'email_create' => string '1' (length=1)
  'is_new_customer' => string '1' (length=1)
  'back' => string 'my-account' (length=10)
  'submitAccount' => string 'Register' (length=8)
</pre>
<p>The post <a rel="nofollow" href="http://nemops.com/validate-junk-addresses-prestashop/">Forbid the usage of junk-alias emails in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/validate-junk-addresses-prestashop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
