如何不需要必需的输入

发布于 2024-10-31 11:57:00 字数 668 浏览 1 评论 0原文

我有一个与用户关联的建筑物User 还可以注册、登录等。我设置了验证集,以便关键的 User 字段(例如 emailname< /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 aUser` 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 技术交流群。

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

发布评论

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

评论(7

浅忆流年 2024-11-07 11:57:00

我想这已经是一个很长的时间了,但这里是使输入元素不需要的“正确”方法(至少在 Cake 2.4.1 中):

echo $this->Form->input('studentid', array(
    'label' => __('Student ID'),
    'required' => false
));

只需传递 '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):

echo $this->Form->input('studentid', array(
    'label' => __('Student ID'),
    'required' => false
));

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.

゛清羽墨安 2024-11-07 11:57:00

我遇到了同样的问题,这对我有用(在 Cake 1.2 中测试过,但我确信它会转换为 1.3)

  1. 在标签中添加一个“norequire”类:

    echo $this->Form->input( 'User.first_name', array('label'=>array('class'=>'norequire','text'=>; '名') ));
    
  2. 在 CSS 中,设置 norequire 类:

    form .required label.norequire { 字体粗细:正常; }
    表单 .required label.norequire:after { 内容:''; }
    

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

  1. Add a "norequire" class to your label :

    echo $this->Form->input( 'User.first_name', array('label'=>array('class'=>'norequire','text'=>'First Name') ));
    
  2. In your CSS, set up the norequire class:

    form .required label.norequire { font-weight:normal;  }
    form .required label.norequire:after { content:'';  }
    

(The "form .required" part was important for overriding cakes's default css for the required class. )

仙气飘飘 2024-11-07 11:57:00

这应该可以做到:

echo $this->Form->input('User.first_name',
                        array('div' => array('class' => 'input text')));

或者,您可以在控制器中unset仅针对该视图的required规则,但要小心结果:

unset($this->User->validate['first_name']['ruleName']['required']);

This should do it:

echo $this->Form->input('User.first_name',
                        array('div' => array('class' => 'input text')));

Alternatively, you could unset the required rule in the controller just for that view, but be careful with the results:

unset($this->User->validate['first_name']['ruleName']['required']);
蓝眼睛不忧郁 2024-11-07 11:57:00

我很惊讶 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.

亢潮 2024-11-07 11:57:00

我最终手动添加了部门。不是很优雅,但它有效:

<?php if ($this->Form->isFieldError('first_name')) { ?>
    <div class="input text error">
<?php } else { ?>
    <div class="input text">
<?php }
    echo $this->Form->input('first_name', array('div' => false)); ?>
</div>

I ended up by manually adding the division. Not very graceful but it works:

<?php if ($this->Form->isFieldError('first_name')) { ?>
    <div class="input text error">
<?php } else { ?>
    <div class="input text">
<?php }
    echo $this->Form->input('first_name', array('div' => false)); ?>
</div>
猫卆 2024-11-07 11:57:00
$("#idOfYourTag").attr("required","false");

这对我有用

$("#idOfYourTag").attr("required","false");

This works for me

孤独难免 2024-11-07 11:57:00

我仍然没有找到一个“正确”的答案,但作为一个快速破解,你可以尝试不使用表单助手并将代码放入自己

<?php
//echo $this->Form->input( 'User.first_name' )
//echo $this->Form->input( 'User.last_name' )
//echo $this->Form->input( 'User.email' )
echo "<div class='input text'><label for='User_first_name'>First Name</label>
      <input name='data[User][first_name]' maxlength='50' type='text' id='User_first_name'/></div>";
echo "<div class='input text'><label for='User_last_name'>Last Name</label>
      <input name='data[User][last_name]' maxlength='50' type='text' id='User_last_name'/></div>";
echo "<div class='input text'><label for='User_email'>Username</label>
      <input name='data[User][email]' maxlength='50' type='text' id='User_email'/></div>";
?>

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

<?php
//echo $this->Form->input( 'User.first_name' )
//echo $this->Form->input( 'User.last_name' )
//echo $this->Form->input( 'User.email' )
echo "<div class='input text'><label for='User_first_name'>First Name</label>
      <input name='data[User][first_name]' maxlength='50' type='text' id='User_first_name'/></div>";
echo "<div class='input text'><label for='User_last_name'>Last Name</label>
      <input name='data[User][last_name]' maxlength='50' type='text' id='User_last_name'/></div>";
echo "<div class='input text'><label for='User_email'>Username</label>
      <input name='data[User][email]' maxlength='50' type='text' id='User_email'/></div>";
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文