我的 HTML 中有一组复选框,如下所示,
div class="grid_7">
<fieldset class="age shadow_50">
<label class="legend">Age Group</label>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="child" />
<label>Child</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="30's" />
<label>30s</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="60's" />
<label>60's</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="teen" />
<label>Teen</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="40's" />
<label>40's</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="70's" />
<label>70's</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="20's" />
<label>20's</label>
</div>
<div class="formRow checkbox">
<input id="" type="checkbox" name="age[]" value="50's" />
<label>50's</label>
</div>
</fieldset>
</div>
我在我的 Controller 中设置了一个验证规则(在我看来)确保复选框已被选中,
$this->form_validation->set_rules('age[]', 'age group', 'required|trim');
然而,在测试这个时,我收到一条错误消息,显示名称为 age[] 的复选框,我只想检查 age[] 是否为空。
我怎样才能做到这一点?
最佳答案
你不能像那样测试 age[],它是一个数组。有几种方法可以做到这一点,但您所做的 required 不是其中之一。
您可以使用 javascript 或通过您自己的自定义运行 age[] 值 callback function是一种方法。
在 CI 中使用数组的详细信息在这里:
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields
如果您使用 javascript 路由,您可以使用 jQuery 遍历您的复选框(使用一个类来链接它们)并确保其中 1 个被选中。
关于php - codeigniter 表单验证和数组作为字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727048/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser