How to not require a required input

Rob Wilkerson picture Rob Wilkerson · Apr 11, 2011 · Viewed 7.2k times · Source

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.

Answer

Jackson picture Jackson · Oct 25, 2013

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.