vendredi 4 septembre 2015

Laravel keep chosen select value

I am building an app in Laravel that uses the jQuery Chosen plugin. When a user submits a form I am using requests to validate the form. However if they are redirected back, for example if they miss a required field, then the Chosen select field does not keep their value.

My code is below. What am I missing?

Controller

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return view('admin.newContractor')
        ->with('timesheetTemplates', $this->timesheet->listTemplates())
        ->with('supervisors', $this->user->getUsersByRole($this->role->findRoleByName('Supervisor')->id)->lists('email', 'id'))
        ->with('contractorRoleId', $this->role->findRoleByName('Contractor')->id)
        ->with('rateFields', $this->rate->all())
        ->with('miscFields', $this->misc->all())
        ->with('workTypes', $this->timesheet->getTypeWork());
}

View

{!! Form::open([]) !!}
...

    @foreach($rateFields as $rate)
        {!! Form::label($rate->field, $rate->name) !!}
        {!! Form::text($rate->field, '') !!}
    @endforeach

... 
{!! Form::close([]) !!}

Request

<?php

namespace App\Http\Requests\Admin;

use App\Http\Requests\Request;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Factory as ValidatorFactory;
use String;

class CreateUserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name'        => 'required',
            'last_name'         => 'required',
            'email'             => 'required|unique:user,email',
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function validator(ValidatorFactory $factory)
    {
        if(!$this->get('status'))
        {
            $this->merge(['activation_code' => String::random(30)]);
        }

        return $factory->make($this->input(), $this->rules(), $this->messages());
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VBNqCU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire