# How to validate a form field sequentially

**URL:** <https://www.ruby-forum.com/t/how-to-validate-a-form-field-sequentially/165500>\
**Category:** Rails\
**Created:** [April 14, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-form-field-sequentially/165500 "2009-04-14T16:11:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [April 14, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-form-field-sequentially/165500/1 "2009-04-14T16:11:35Z")

</div>

First, sorry for my English :S …

Hi! I’ve searched about my problem into the forum but I didn’t find any  
post.  
When I fill, for example, an email field my model validates the  
presence, uniqueness, max-length (128) and format (a regular  
expression).  
If a user doesn’t fill that field, the validation returns two messages:

- The message about the field is blank (yeeeee).
- The message about the field haven’t the correct format (buuuu).  
I only want to show the first message. Is there any way to do the  
validation sequentially and stop when a validation doesn’t be correct?  
an example: first =\> presence, second =\> format, third =\> max-length,  
fourth =\> uniqueness…

Last, sorry for my English :S …  
Thanks!

Luis

---

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [April 14, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-form-field-sequentially/165500/2 "2009-04-14T16:32:35Z")

</div>

I’ve tought a solution, when it validates, it only takes the first error  
message:

```
if obj.errors.on(:email)
errMsg << obj.errors.on(:email)[0]
end

```

Anybody knows a more elegant solution?  
greetings!  
Luis.

Luis SÃ¡nchez wrote:

> First, sorry for my English :S …
> 
> Hi! I’ve searched about my problem into the forum but I didn’t find any  
> post.  
> When I fill, for example, an email field my model validates the  
> presence, uniqueness, max-length (128) and format (a regular  
> expression).  
> If a user doesn’t fill that field, the validation returns two messages:
> 
> - The message about the field is blank (yeeeee).
> - The message about the field haven’t the correct format (buuuu).  
> I only want to show the first message. Is there any way to do the  
> validation sequentially and stop when a validation doesn’t be correct?  
> an example: first =\> presence, second =\> format, third =\> max-length,  
> fourth =\> uniqueness…
> 
> Last, sorry for my English :S …  
> Thanks!
> 
> Luis

---

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [April 14, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-form-field-sequentially/165500/3 "2009-04-14T17:16:12Z")

</div>

Yes, validating format only when field has a value:

ex.

validates\_format\_of :field, :with =\> “xxxx”, :allow\_blank =\> true

:allow\_nil - If set to true, skips this validation if the attribute is  
nil (default is false)  
:allow\_blank - If set to true, skips this validation if the attribute is  
blank (default is false)

or  
validates\_format\_of :field, :with =\> “xxxx”, :if =\> Proc.new{|record|  
!record.field.blank? }  
but the first one is better (allow\_blank), with the last one u can make  
a validation based on more than one field…

Hope this helps…

---

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [April 14, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-form-field-sequentially/165500/4 "2009-04-14T17:50:20Z")

</div>

Amazing! thanks Gianluca :DD!!

Luis

Gianluca T. wrote:

> Yes, validating format only when field has a value:
> 
> ex.
> 
> validates\_format\_of :field, :with =\> “xxxx”, :allow\_blank =\> true
> 
> :allow\_nil - If set to true, skips this validation if the attribute is  
> nil (default is false)  
> :allow\_blank - If set to true, skips this validation if the attribute is  
> blank (default is false)
> 
> or  
> validates\_format\_of :field, :with =\> “xxxx”, :if =\> Proc.new{|record|  
> !record.field.blank? }  
> but the first one is better (allow\_blank), with the last one u can make  
> a validation based on more than one field…
> 
> Hope this helps…
