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.
Just unset the required attribute. If you're trying to unset it after some user action (with javascript) then use the .
Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.
The required attribute is a boolean attribute. When present, it specifies that an input field must be filled out before submitting the form. Note: The required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.
The :required CSS pseudo-class represents any <input> , <select> , or <textarea> element that has the required attribute set on it. This pseudo-class is useful for highlighting fields that must have valid data before a form can be submitted. Note: The :optional pseudo-class selects optional form fields.
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. )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With