How-to: one form, two submit button, two validates-presence_

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

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 :slight_smile:

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. [email protected]

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. [email protected]

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?

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. [email protected]

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. [email protected]

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])

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. [email protected]

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 :slight_smile: