How can I add a red asterisk next to the label of each required field?

Issue #3 closed
Thomas Georgiadis created an issue

I'd like to add a red asterisk to the right of the label text for each required field.

How can I achieve this?

Comments (6)

  1. lilHermit repo owner

    I'd suggest the best way would be via css rather than messing with the CakePHP templates

    Something like? div.form-group.required:after { content:" *"; color:red; }

  2. Thomas Georgiadis reporter

    It doesn't seem like a required class is being added to the form-group class div

    <div class="form-group <!--required missing?-->">
       <label class="col-form-label" for="person-given-name">Given Name</label>
       <input type="text" name="person[given_name]" required="required" id="person-given-name" class="form-control" value="Test 1">
    </div>
    

    Also wouldn't that css put the asterisk where I've indicated with a black asterisk? Capture.PNG

    Ideally I would like the the asterisk to be right next to the label. I can tweak that css to work if either the form-group or label has a required class.

  3. lilHermit repo owner

    Ok it looks like the plugin isn't adding the required class currently I'll fix that in the next release.

    As a current workaround try:

    <style> label.required:after { content: " *"; color: red; } </style>

    and with the following PHP

    echo $this->Form->control('given_name', [
                        'label' => ['class' => 'required'],
                        'required' => true
                    ]);
    
  4. Log in to comment