eriktobben's avatar

Form validation with old input

Hi!

If validation fails, Laravel wil automatically redirect back with old input and selects when using LaravelCollective HTML. But I have an select/dropdown that uses foreach to populate from DB, and when validation fails, those are reset to the default. How can I get the selected value to be still selected when validation fails?

<div class="col-md-6">
                      <div class="form-group {!! $errors->first('item', 'has-error') !!}">
                           <div class="col-sm-12">
                               {!! Form::label('item', 'Select an item', array('class' => 'control-label')); !!}
                               <select class="form-control" name="item" id="item">
                                   <option disabled selected>Velg...</option>
                                   @foreach ($items as $item)
                                   <option value="{{ $item->name }}">{{ $item->name }}</option>
                                   @endforeach
                               </select>
                           </div>
                       </div>
                    </div>

Also, is it possible to have conditions on validation rules? When a user has selected a certain category, one additional field is presented. How can I validate this field only if a user has selected the spesific category?

0 likes
6 replies
bobbybouwmann's avatar

If you want to set the value to the old value you will have to add it to the select itself

<select class="form-control" name="item" id="item">
    <option disabled selected>Velg...</option>
    @foreach ($items as $item)
        <option value="{{ $item->name }}" @if (old('item') == $item->name) selected="selected" @endif>{{ $item->name }}</option>
    @endforeach
</select>
4 likes
eriktobben's avatar

Thanks! That worked great!

Any thoughts on how to validate fields that are only present if some condition is met?

eriktobben's avatar

Snapey, is it possible to foreach through all the options from DB and display them using Laravel Collective for this?

I am using validation in a Request class. Can I do a check inside that class when its not in my controller?

Snapey's avatar

You don't need to foreach - the form control does it for you (untested)

echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');

so, you have the name of the element 'size' in this case

An array of the options( Large or Small, with return values of L or S)

And the current value (or old value) 'S' - use old($item)

From your controller, you need to supply an array of the key,value pairs for each option. You also need to give the list a different name to the select element, something like $itemList. Then you would end up with something like;

echo Form::select('item', $itemlist, old($item));

See here for further reading : http://laravel-recipes.com/recipes/163/creating-a-select-box-field

Snapey's avatar

Regarding the request class

Note that this

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:5|max:50',
            'email' => 'required|email|min:5|max:50',
        ];
    }

is just a way to return an array. You can put logic in here also. For instance;

    public function rules()
    {
        $ruleset = [
            'name' => 'required|min:5|max:50',
            'email' => 'required|email|min:5|max:50',
        ];

        if($this->method == 'POST') {
            array_add($ruleset,'password','required|min:8');
        }
        return $ruleset;
    }

In this example, I am checking if the request is a POST, this allows me to use the same form request validator for both new user creation and edit of user details (there are actually more fields and rules not shown). The point is, in the rules function I can tinker with the array before returning it.

Please or to participate in this conversation.