Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not require a required input

Tags:

cakephp-1.3

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.

like image 498
Rob Wilkerson Avatar asked Apr 11 '11 00:04

Rob Wilkerson


People also ask

How do you make an input field not required?

Just unset the required attribute. If you're trying to unset it after some user action (with javascript) then use the .

How do you make a field non mandatory in HTML?

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.

How do you set required true in HTML?

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.

How do you style fields required?

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.


2 Answers

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.

like image 136
Jackson Avatar answered Oct 18 '22 04:10

Jackson


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

like image 23
Ella Ryan Avatar answered Oct 18 '22 04:10

Ella Ryan