# How to validate optional fieldset which becomes mandatory?

**URL:** <https://www.ruby-forum.com/t/how-to-validate-optional-fieldset-which-becomes-mandatory/57357>\
**Category:** Rails\
**Created:** [April 10, 2006, 10:13am UTC](https://www.ruby-forum.com/t/how-to-validate-optional-fieldset-which-becomes-mandatory/57357 "2006-04-10T10:13:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [April 10, 2006, 10:13am UTC](https://www.ruby-forum.com/t/how-to-validate-optional-fieldset-which-becomes-mandatory/57357/1 "2006-04-10T10:13:44Z")

</div>

Hi,

I am posting here because a re-read of my ‘Agile’ book and quick google  
around didn’t give me any obvious clues, but I apologise if this has  
already been asked and answered before…

I have an ‘invoice address’ fieldset in a form which is entirely  
optional,  
however, should the user fill any one of the fields then they  
effectively  
all become mandatory and I need to validate the presence of the required  
fields (I need to make sure that at least the first line of address,  
city  
and zip are filled).

What is the best way to do this? I am new to Rails and still finding my  
feet but I appreciate that there is probably more than one way of  
approaching this?

TIA,

Matt.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [April 10, 2006, 10:21am UTC](https://www.ruby-forum.com/t/how-to-validate-optional-fieldset-which-becomes-mandatory/57357/2 "2006-04-10T10:21:48Z")

</div>

Matthew L. wrote:

> Hi,
> 
> I am posting here because a re-read of my ‘Agile’ book and quick google  
> around didn’t give me any obvious clues, but I apologise if this has  
> already been asked and answered before…
> 
> I have an ‘invoice address’ fieldset in a form which is entirely  
> optional,  
> however, should the user fill any one of the fields then they  
> effectively  
> all become mandatory and I need to validate the presence of the required  
> fields (I need to make sure that at least the first line of address,  
> city  
> and zip are filled).
> 
> What is the best way to do this? I am new to Rails and still finding my  
> feet but I appreciate that there is probably more than one way of  
> approaching this?
> 
> TIA,
> 
> Matt.

AWDW page 267 describes the “validate” method, where you can put any  
custom validations that don’t fit the higher-level validations provided.

So you can do this:

class Invoice \< ActiveRecord::Base  
def validate  
if address or city or zip  
errors.add\_to\_base(“You must specify address, city and zip for  
this invoice”) unless (address and city and zip)  
end  
end  
end

That’s a rough idea of how you could do it. Hope it helps!

Jeff

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [April 10, 2006, 11:08am UTC](https://www.ruby-forum.com/t/how-to-validate-optional-fieldset-which-becomes-mandatory/57357/3 "2006-04-10T11:08:56Z")

</div>

I addition to Jeff’s advise, I would put address in separate table/class  
where all kinds of addresses go. So you will have a link from your  
Invoice to the Address, as BillingAddress, ShipAddress etc. This will  
also allow to use standard validators.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [April 10, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/how-to-validate-optional-fieldset-which-becomes-mandatory/57357/4 "2006-04-10T13:55:39Z")

</div>

> I addition to Jeff’s advise, I would put address in separate table/class  
> where all kinds of addresses go. So you will have a link from your  
> Invoice to the Address, as BillingAddress, ShipAddress etc. This will  
> also allow to use standard validators.

Thanks guys. That was the push I needed to get it working and a big  
“D’oh!” for not realising I should be using validate to get this done.

Thanks!
