单选按钮和文本框验证 Ruby on Rails

发布于 2024-09-24 13:46:16 字数 1928 浏览 0 评论 0原文

这是我目前的设置:

<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="Male"> Male
<input id="demographics_questionary_gender_gender" name="demographics_questionary[gender]" type="radio" value="Female"> Female
<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="Other"> Other
<input id="other_gender_value" name="other_gender[value]" size="30" type="text">

这将产生三个单选按钮(男性、女性、其他)。如果选择“其他”单选按钮,还会出现一个文本框。

我的问题涉及如果选择“其他”单选按钮,如何验证文本框是否已填写。

目前,我所做的是在控制器中检查是否选择了另一个单选按钮,如果是,那么我会将存储的值更改为“其他”文本框中设置的值。但是,我真的不知道如何验证文本框。我知道如何通过验证模型中是否设置了值来验证单选按钮是否被选中,但不知道如何确保用户已将数据输入到“其他”文本框中。

以下是控制器中的代码片段:

if @demographics_questionary.gender == 'Other'
    @other_gender = params[:other_gender]
    if @other_gender[:value].nil?
        @demographics_questionary.gender  = @other_gender[:value]
    else 
        @demographics_questionary.gender  = 'Not Set'
    end
 end

如您所见,如果选择了“其他”单选按钮并且“其他”文本框为空,我目前只是将其设置为“未设置”。但是,当选择按钮来处理数据时,就会执行此代码。理想情况下,我希望在视图页面上进行验证,如果选择了“其他”单选按钮,则在填充文本框之前,用户将无法继续前进。这样,当它甚至到达控制器方法时,就不需要进行此检查了。

但我再次对建议的任何其他方法持开放态度。

我认为这可以通过 JavaScript 或 jQuery 完成,但我不知道如何完成。

哦,是的,所以视图中的所有这些代码都位于表单字段中:

<% form_for(@demographics_questionary) do |f| %>
  <%= f.error_messages :header_message => "Please do not skip any of the questions." %>
  /* Code for radio buttons */
  <%= f.submit 'Next' %>   

 <%end%>

请注意,单选按钮代码位于 form_for 中。一旦触发提交按钮,它将执行控制器中的方法。如果您要使用 JavaScript/jQuery,您将如何将函数附加到提交按钮?

这是当前页面:

http://otoplusvn.com/TherapistSurvey/demgraphics_questionaries/new关于性别的问题2。

有什么建议吗?我很感激,谢谢 德里克

This is currently the setup I have:

<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="Male"> Male
<input id="demographics_questionary_gender_gender" name="demographics_questionary[gender]" type="radio" value="Female"> Female
<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="Other"> Other
<input id="other_gender_value" name="other_gender[value]" size="30" type="text">

This will produce three radio buttons (Male, Female, Other). There is also a text box if the "Other" radio button is selected.

My question deals with how to validate if the text box is filled in if the "Other" radio button is selected.

Currently, what I do is in the controller I check if the other radio button is selected and if it is then I'll change the stored value to the value set in the "other" text box. However, I don't really know how to validate the text box. I know how to validate if a radio button is checked by verifying that a value has been set in the model, but not how to make sure the user had inputed data into the "Other" text box.

Here is the snippet of code from the controller:

if @demographics_questionary.gender == 'Other'
    @other_gender = params[:other_gender]
    if @other_gender[:value].nil?
        @demographics_questionary.gender  = @other_gender[:value]
    else 
        @demographics_questionary.gender  = 'Not Set'
    end
 end

As you can see, I currently just set it to 'Not Set' if the 'Other' radio button is selected and the 'Other' text box is empty. However, this code is executed when a button is selected to process the data. Ideally, I would want the validation to be done at the view page where it would prevent the user from going forward until the text box is filled in if the "Other" radio button is selected. In that way, by the time it even hits the controller method this check would not be required.

But again I'm open to any other methods suggested.

I'm thinking this can be done JavaScript or jQuery but I'm not sure how.

Oh yeah, so all this code in the view lies in a form field:

<% form_for(@demographics_questionary) do |f| %>
  <%= f.error_messages :header_message => "Please do not skip any of the questions." %>
  /* Code for radio buttons */
  <%= f.submit 'Next' %>   

 <%end%>

Notice the radio button code is located in a form_for. Once the submit button is triggered then it'll execute the method in the controller. If you were to use JavaScript/jQuery how would you append the function to the submit button?

This is the current page :

http://otoplusvn.com/TherapistSurvey/demographics_questionaries/new for question 2 on gender.

Any suggestions? I appreciate it, Thanks
Derek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2024-10-01 13:46:16

您可以添加依赖关系规则,如下所示:

$('#register_form').validate({rules: {
     'other_gender[value]': {
       required: {
         depends: function(element) {
           return $("input[name=demographics_questionary[gender]]:checked").val() == "Other"
         }
       }
     }
} });

仅当所选选项为“其他”时,才需要文本框

You can add dependency rules, something like this:

$('#register_form').validate({rules: {
     'other_gender[value]': {
       required: {
         depends: function(element) {
           return $("input[name=demographics_questionary[gender]]:checked").val() == "Other"
         }
       }
     }
} });

That will require the text box, only if the selected option is "Other"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文