# Validate field with radio\_button

**URL:** <https://www.ruby-forum.com/t/validate-field-with-radio-button/150931>\
**Category:** Rails\
**Created:** [October 14, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/validate-field-with-radio-button/150931 "2008-10-14T23:42:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a698b9/32.png) [@fmh](https://www.ruby-forum.com/u/fmh)\
**Post date:** [October 14, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/validate-field-with-radio-button/150931/1 "2008-10-14T23:42:15Z")

</div>

hello,

i want validate my text\_field only when the radio\_button is clicked.  
i try this but when i validate my form , the radio button lost focus  
and validate is not checked

—View----  
\<%= radio\_button(“user”,“type”,  
“private”,:onclick=\>“Element.hide(‘compagny’)”) %\>Private  
\<%= radio\_button (“user”,“type”,  
“compagny”,:onclick=\>“Element.show(‘compagny’)”, :onchange=\>  
visual\_effect(:Highlight, “compagny”))%\>Compagny

—model----

validates\_presence\_of :compagny\_name , :if =\> :type\_required?

def type\_required?  
return true unless type = “compagny”  
end
