質問

I have a Laravel form that gets passed through some validation. Obviously, if the validation fails, I'd like the user to be redirected to form and alerted of their mistake. Also, because the form is fairly large, I don't want the user to have to re-input any data. Here is the form (I've cut off a large part to reduce the size of this message)

:@layout('templates.main')

@section('content')
    @if(Session::has('validation_errors'))
    <ul class="form_errors">
        @foreach($errors as $error)
            {{$error}}
        @endforeach
    </ul>
    @endif

    {{ Form::open('account/create', 'POST') }}
    <table>
        <tr>
            <td>{{ Form::label('dealer', 'Dealer') }}</td>
            <td>{{ Form::select('dealer', array('1' => 'Dealer #1', '2' => 'Dealer #2')) }}</td>
            <td>{{ Form::label('amount', 'Amount') }}</td>
            <td>{{ Form::text('amount', Input::old('amount')) }}</td><!--Here is where I'm testing for old input-->
        </tr>
        <tr>
            <td>{{ Form::label('date', 'Date') }}</td>
            <td>{{ Form::date('date', NULL, array('class' => 'date_picker')) }}</td>
            <td>{{ Form::label('installation', 'Installation #') }}</td>
            <td>{{ Form::input('text', 'installation') }}</td>
        </tr>
        <tr>
            <td colspan="4">{{ Form::textarea('notebox', NULL, array('id' => 'notebox')) }}</td>
        </tr>
        <tr>
            <td colspan="4">{{ Form::submit('Submit') }}&nbsp;{{ Form::reset('Reset') }}</td>
        </tr>
    </table>
    {{ Form::close() }}
@endsection

When the form is submitted, here is the controller that handles it:

public function post_create() {
        //Validate it in the model
        $validation = Account::validate(Input::all());
        if($validation->fails()){
            return Redirect::to('account/create')
                ->with_input()
                ->with('validation_errors', true)
                ->with('errors', $validation->errors->all('<li>:message</li>'));
        }
        else {
            return "passed";//for debugging purposes
        }
    }

And lastly, here is the Account model:

class Account extends Eloquent { 
        public static function validate($input) {
            //Validation rules
            $rules = array(
                'firstname' => 'required',
                'lastname' => 'required',
                            //etc...
            );
            //Custom validation messages
            $messages = array(
                'firstname_required' => 'A first name is required.',
                'lastname_required' => 'A last name is required.',
                //etc...
            );
            //Pass it through the validator and spit it out
            return Validator::make($input, $rules, $messages);
        }
    }

So when I go to test this by intentionally making the form invalid, I am redirected to the account/create form, however, no validation errors show, and the old input is not saved (I only attached Input::old() to the amount textbox).

役に立ちましたか?

解決

It turns out that if the total number of characters from Input::old() exceeds ~1400 characters, things get a little buggy. I solved this by changing from 'driver' => 'cookie' to 'driver' => 'file' in my application/config/session.php and it began to work as expected.

https://github.com/laravel/laravel/issues/1475

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top