How to not require a required input
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.
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.
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 :
echo $this->Form->input( 'User.first_name', array('label'=>array('class'=>'norequire','text'=>'First Name') ));
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. )
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']);
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:
<?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>
$("#idOfYourTag").attr("required","false");
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
<?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>";
?>
精彩评论