# How-to: one form, two submit button, two validates-presence\_

**URL:** <https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944>\
**Category:** Rails\
**Created:** [March 15, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944 "2007-03-15T19:57:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 15, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/1 "2007-03-15T19:57:13Z")

</div>

Hi, everybody;

The following question blocked my way for a couple days. I appreciate  
any help in advance.

I have one form that adds text entry to the database. There are two  
ways to add the entry:

1. User fills all the fields then click “submit button 1” – need to  
validates\_presence\_of “field-1” and “field-2”

2. User click “submit button 2” – need to validates\_presence\_of  
“field-2” and “field-5”. (“field-5” contains a URL and server will  
search in the internet and fill all other fields automatically)

How to implement the validations?

Thanks again

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 15, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/2 "2007-03-15T20:23:57Z")

</div>

zhiyong wrote:

> Hi, everybody;
> 
> The following question blocked my way for a couple days. I appreciate  
> any help in advance.
> 
> I have one form that adds text entry to the database. There are two  
> ways to add the entry:
> 
> 1. User fills all the fields then click “submit button 1” – need to  
> validates\_presence\_of “field-1” and “field-2”
> 
> 2. User click “submit button 2” – need to validates\_presence\_of  
> “field-2” and “field-5”. (“field-5” contains a URL and server will  
> search in the internet and fill all other fields automatically)
> 
> How to implement the validations?
> 
> Thanks again

validates presence takes some options, and one of them you can use is  
“if”, to check which button has been clicked 🙂

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 15, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/3 "2007-03-15T20:41:23Z")

</div>

I have tried the :if option but how to pass the button name to the  
model? I used params[:create], it doesn’t work

On Mar 15, 3:23 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 15, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/4 "2007-03-15T21:37:38Z")

</div>

zhiyong wrote:

> I have tried the :if option but how to pass the button name to the  
> model? I used params[:create], it doesn’t work
> 
> On Mar 15, 3:23 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Well in this case you can sent different buttons to different controller  
but this is another solution.

In your case now you should be able to check which button is clicked by  
using the button name, I think…can you show us your codes?

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 15, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/5 "2007-03-15T20:42:23Z")

</div>

I have tried the :if option but how to pass the button name to the  
model? I used params[:create], it doesn’t work

On Mar 15, 3:23 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 16, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/6 "2007-03-16T16:42:36Z")

</div>

zhiyong wrote:

> here are the several ways I tried in model class:
> 
> 1. validates\_presence\_of :label, :tag, :if =\>params[:create] ==  
> “Create”  
> validates\_presence\_of :label, :url, :if =\>params[:create] ==  
> “Get text and save”  
> (it seems that model does not recognize the params[])
> 
> 2. validates\_presence\_of :label, :tag, if =\> create == “Create”  
> validates\_presence\_of :label, :url, :if =\> create == “Get text  
> and save”  
> (no run-time error, but the validation result is not correct)
> 
> 3. validates\_presence\_of :label, :tag, if =\> :create == “Create”  
> validates\_presence\_of :label, :url, :if =\> :create == “Get text  
> and save”  
> (same as 2)
> 
> The “create” is the submit button name and it is not related with the  
> database.
> 
> I have also written a method in model class, with the  
> validates\_presence\_of in it, then called from the controller. But the  
> error is that “validates\_presence\_of” is not a method. it is very  
> strange.
> 
> On Mar 15, 4:37 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

It seems that - the model cannot access the params, so you need to make  
method in your model to recieve the params from your controller.

model

def validate(button)  
do this for that button and do that for that button  
end

controller

validate(params[:create])

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 15, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/7 "2007-03-15T22:07:37Z")

</div>

here are the several ways I tried in model class:

1. validates\_presence\_of :label, :tag, :if =\>params[:create] ==  
“Create”  
validates\_presence\_of :label, :url, :if =\>params[:create] ==  
“Get text and save”  
(it seems that model does not recognize the params[])

2. validates\_presence\_of :label, :tag, if =\> create == “Create”  
validates\_presence\_of :label, :url, :if =\> create == “Get text  
and save”  
(no run-time error, but the validation result is not correct)

3. validates\_presence\_of :label, :tag, if =\> :create == “Create”  
validates\_presence\_of :label, :url, :if =\> :create == “Get text  
and save”  
(same as 2)

The “create” is the submit button name and it is not related with the  
database.

I have also written a method in model class, with the  
validates\_presence\_of in it, then called from the controller. But the  
error is that “validates\_presence\_of” is not a method. it is very  
strange.

On Mar 15, 4:37 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zhiyong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecc23a/32.png) [@zhiyong](https://www.ruby-forum.com/u/zhiyong)\
**Post date:** [March 29, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/how-to-one-form-two-submit-button-two-validates-presence/88944/8 "2007-03-29T23:40:40Z")

</div>

zhiyong wrote:

> Hi, everybody;
> 
> The following question blocked my way for a couple days. I appreciate  
> any help in advance.
> 
> I have one form that adds text entry to the database. There are two  
> ways to add the entry:
> 
> 1. User fills all the fields then click “submit button 1” – need to  
> validates\_presence\_of “field-1” and “field-2”
> 
> 2. User click “submit button 2” – need to validates\_presence\_of  
> “field-2” and “field-5”. (“field-5” contains a URL and server will  
> search in the internet and fill all other fields automatically)
> 
> How to implement the validations?
> 
> Thanks again

I’m back to you, while I was working with the validation thing, I wanted  
to hide some errors if some other is shown.

Check if the following input is blank  
validates\_presence\_of :nickname, :email, :password

Only check the nickname between 4 and 40 IF the nickname is not blank  
validates\_length\_of :nickname, :within =\> 4…40, :if =\> Proc.new { |u|  
!u.nickname.blank? }

Hope you can use this 🙂
