Search code examples
phplaravellaravelcollective

Value '0' not shown in Form::label


So I have been trying to put a zero in this label in many different ways.

Here is what I am trying to do.

This label is supposed to hold the value of an anchor. This anchor determents how heavy they tag should weight. (making sorting easier and stuff.)

Now all tags get a default value of zero. So this is what the label has to hold. However, whenever I put 0 in as the value it will display the name of the label instead.

Here are the things i tried:

{{Form::label($tag->value . '_anchor', '0')}}
{{Form::label($tag->value . '_anchor', 0)}}
{{Form::label($tag->value . '_anchor', "0")}}

even tryed fooling it:

{{Form::label($tag->value . '_anchor', function(){
                            return 0;})}}

This is the raw code as requested:

<div class="form-group">
        @if(count($tags) > 0)
            <div class="col-md-2">
                @foreach($tags as $tag)
                    <div class="tags" style="float: none;">
                        {{Form::label($tag->value, $tag->value)}}
                        {{Form::checkbox($tag->value, $tag->id, false)}}
                        <div>
                            <span class="glyphicon glyphicon-play reverse"></span>
                            {{Form::label($tag->value . '-anchor', 1)}}
                            {{Form::hidden($tag->value . '_anchor', 0)}}
                            </span><span class="glyphicon glyphicon-play"></span>
                        </div>

                    </div>
                @endforeach
            </div>
        @endif

    </div>

Any idea how to get a zero value in my label? Any other number displays fine.

Here is a picture of what I got and what I expect. As seen with 1 it works fine. enter image description here

Hope you guys can help. If you need any more code or information please ask.


Solution

  • Laravel runs label names through formatLabel:

    protected function formatLabel($name, $value)
    {
        return $value ?: ucwords(str_replace('_', ' ', $name));
    }
    

    Because this check uses non-strict comparison (== instead of ===), $value being zero is seen as a false (false-y!) value, and thus it uses the first parameter of your label call instead of the second.

    (The point of Laravel doing this is so you can omit the second parameter entirely, and it'll just use the field's name to make a best guess at what the label should say.)

    Your easiest bet here is to just output the <label for="foo_anchor">0</label> raw HTML yourself.