<?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>La rolls des blogs &#187; model</title>
	<atom:link href="http://my.rails-royce.org/tag/model/feed/" rel="self" type="application/rss+xml" />
	<link>http://my.rails-royce.org</link>
	<description>Another Ruby and Rails blog</description>
	<lastBuildDate>Fri, 13 Jan 2012 23:47:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Email validation in Ruby On Rails 3 or Active model without regexp</title>
		<link>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/</link>
		<comments>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 08:21:42 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[email]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[3.0]]></category>
		<category><![CDATA[active model]]></category>
		<category><![CDATA[activemodel]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[rails-3]]></category>
		<category><![CDATA[rails-3.0]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rails3.0]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[validate]]></category>
		<category><![CDATA[validations]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=186</guid>
		<description><![CDATA[In Rails 3.0, you can use custom validator in your active_record model. So I wanted to manage email validations without regexp matching like others do. I find a new way to make this work thanks to ruby mail gem, a dependency of Rails 3.0 I use this with devise, see my blogpost here : Ruby &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>In Rails 3.0, you can use custom validator in your active_record model.<br />
So I wanted to manage email validations without regexp matching like others do.<br />
I find a new way to make this work thanks to ruby mail gem, a dependency of Rails 3.0</p>
<p>I use this with devise, see my blogpost here : <a href="http://my.rails-royce.org/2010/10/20/rails-how-to-skip-or-remove-validations-in-a-model/">Ruby Rails : How to bypass skip validation in Devise</a></p>
<p>Edit : Here is a gem <a href="https://github.com/hallelujah/valid_email" title="Github repository">https://github.com/hallelujah/valid_email</a></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just put a file in app/validators/email_validator.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mail'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> EmailValidator <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::EachValidator</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> validate_each<span style="color:#006600; font-weight:bold;">&#40;</span>record,attribute,value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      m = <span style="color:#6666ff; font-weight:bold;">Mail::Address</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We must check that value contains a domain and that value is an email address</span>
      r = m.<span style="color:#9900CC;">domain</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> m.<span style="color:#9900CC;">address</span> == value
      t = m.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tree</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We need to dig into treetop</span>
      <span style="color:#008000; font-style:italic;"># A valid domain must have dot_atom_text elements size &gt; 1</span>
      <span style="color:#008000; font-style:italic;"># user@localhost is excluded</span>
      <span style="color:#008000; font-style:italic;"># treetop must respond to domain</span>
      <span style="color:#008000; font-style:italic;"># We exclude valid email values like &lt;user@localhost.com&gt;</span>
      <span style="color:#008000; font-style:italic;"># Hence we use m.__send__(tree).domain</span>
      r <span style="color:#006600; font-weight:bold;">&amp;&amp;</span>= <span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">domain</span>.<span style="color:#9900CC;">dot_atom_text</span>.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e   
      r = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    record.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span>attribute<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;is invalid&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> r
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>No regexp !! And beautiful !!</p>
<p>Here is the version without activerecord</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_model'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/all'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_model/validations'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mail'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> EmailValidator <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::EachValidator</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> validate_each<span style="color:#006600; font-weight:bold;">&#40;</span>record,attribute,value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      m = <span style="color:#6666ff; font-weight:bold;">Mail::Address</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We must check that value contains a domain and that value is an email address</span>
      r = m.<span style="color:#9900CC;">domain</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> m.<span style="color:#9900CC;">address</span> == value
      t = m.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tree</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We need to dig into treetop</span>
      <span style="color:#008000; font-style:italic;"># A valid domain must have dot_atom_text elements size &gt; 1</span>
      <span style="color:#008000; font-style:italic;"># user@localhost is excluded</span>
      <span style="color:#008000; font-style:italic;"># treetop must respond to domain</span>
      <span style="color:#008000; font-style:italic;"># We exclude valid email values like &lt;user@localhost.com&gt;</span>
      <span style="color:#008000; font-style:italic;"># Hence we use m.__send__(tree).domain</span>
      r <span style="color:#006600; font-weight:bold;">&amp;&amp;</span>= <span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">domain</span>.<span style="color:#9900CC;">dot_atom_text</span>.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      r = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    record.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span>attribute<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;is invalid&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> r
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Person
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Validations</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>
&nbsp;
  validates <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:length</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  validates <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

