如何不需要必需的输入
我有一个与用户
关联的建筑物
。 User
还可以注册、登录等。我设置了验证集,以便关键的 User
字段(例如 email
、name< /code> 等)是必需的。
当我创建建筑物时,我还提供了当场关联用户的功能。我的建筑表单具有该关键用户信息的输入:
<?php echo $this->Form->input( 'User.first_name' ) ?>
<?php echo $this->Form->input( 'User.last_name' ) ?>
<?php echo $this->Form->input( 'User.email' ) ?>
但是,我不希望这些输入被指示为必需的 b/c 我希望用户能够创建一个建筑而不必创建一个
用户记录。我找不到办法从验证规则放置的 div 中删除所需的类。
我尝试了 'required' => 的各种组合false
并设置 class
值,但到目前为止没有任何效果。有没有好的方法来取消表单输入?
谢谢。
I have a Building
that is associated with a User
. A User
can also register, login, etc. I have my validation set so that key User
fields (e.g. email
, name
, etc.) are required.
When I create a building, I'm also offering the ability to associate a user on the spot. My building form has inputs for that key user info:
<?php echo $this->Form->input( 'User.first_name' ) ?>
<?php echo $this->Form->input( 'User.last_name' ) ?>
<?php echo $this->Form->input( 'User.email' ) ?>
However, I don't want those inputs to be indicated as required b/c I want the user to be able to create a Building without necessarily creating a
User` record. What I can't find a way to do is to remove the required class from the div that is being put there by the validation rule.
I've tried various combinations of 'required' => false
and setting the class
value, but nothing has worked so far. Is there a good way to un-require a form input?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想这已经是一个很长的时间了,但这里是使输入元素不需要的“正确”方法(至少在 Cake 2.4.1 中):
只需传递
'required' =>;假
。我真的希望我能说我知道如何自动触发此行为,但修改我的模型似乎不会影响自动生成的
元素。如果/当我弄清楚时,我会更新这篇文章。
I guess this has been a-long-time-comin', but here is the "correct" way to make an input element not required (at least in Cake 2.4.1):
Simply pass
'required' => false
.I really wish I could say I knew how to trigger this behavior automatically, but modifying my models doesn't seem to affect the automatically-generated
<input>
elements. I'll update this post if/when I figure it out.我遇到了同样的问题,这对我有用(在 Cake 1.2 中测试过,但我确信它会转换为 1.3)
在标签中添加一个“norequire”类:
在 CSS 中,设置 norequire 类:
“form .required”部分对于覆盖 cakes 所需类的默认 css 非常重要。)
I had the same problem and this worked for me (tested in Cake 1.2 but I'm sure it will translate to 1.3)
Add a "norequire" class to your label :
In your CSS, set up the norequire class:
(The "form .required" part was important for overriding cakes's default css for the required class. )
这应该可以做到:
或者,您可以在控制器中
unset
仅针对该视图的required
规则,但要小心结果:This should do it:
Alternatively, you could
unset
therequired
rule in the controller just for that view, but be careful with the results:我很惊讶 deceze 的解决方案对我不起作用(也许我只是做错了什么),但我最终不得不使用 Javascript“手动”从每个字段的包含 div 中删除所需的类。
I'm surprised that deceze's solution didn't work for me (maybe I just did something wrong), but I ended up having to use Javascript to "manually" remove the required class from each field's containing div.
我最终手动添加了部门。不是很优雅,但它有效:
I ended up by manually adding the division. Not very graceful but it works:
这对我有用
This works for me
我仍然没有找到一个“正确”的答案,但作为一个快速破解,你可以尝试不使用表单助手并将代码放入自己
I Still haven't found a 'proper' answer for this, but as a quick hack you can just try not using the form helper and throw the code in yourself