Laravel 4 - 发送失败提交时的输入 [英] Laravel 4 - Sending the input back on a failed submission

查看:170
本文介绍了Laravel 4 - 发送失败提交时的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式

  @extends('index')

@section('主')
< h1>创建新的投票< / h1>
{{Form :: model(new Poll,['route'=>'polls.store','class'=>'create-poll'])}}
< div class = 灰盒 >
{{Form :: label(topic,Write your poll question)}}
{{Form :: text('topic','',['placeholder'=>'例如:什么是最好的数字?','id'=>'topic'])}}
< / div>

< div class =gray-box>
{{Form :: label(null,写出可能的答案)}}
{{Form :: text('option []',null,['class'=>'option ','placeholder'=>'Test option'])}}

< input type =buttonclass =more-optionsvalue =Add another option>
{{ - 这会创建另一个选项[] input element - }}

< / div>

{{Form :: submit()}}
{{Form :: close()}}
@stop

还有更多,但这是重要的一部分。基本上它是一个民意调查的名字。它有一个主题和至少一个选项。点击一个按钮,您可以添加更多选项。



这是控制器:

  public function store(){
$ data = Input :: all();

//验证输入
$ validator = Validator :: make($ data,Poll :: $ rules);
if($ validator-> fails())
return Redirect :: back() - > withErrors($ validator) - > withInput();

...

这里的问题是 WithInput()会抛出一个错误:

lockquote

ErrorException

htmlentities()期望参数1是字符串,给定的数组(View:/home/dbugger/laravelproject/app/views/polls/create.blade.php)


我怀疑是因为我使用了一个 Array Form Element ,但我不知道为什么或者如何,因为当时我甚至没有尝试 - 填写表单(失败)提交的数据...

解决方案

对于像的分组输入:: text('option []')... Form :: checkbox('options []')... 你需要在您的控制器中重新排列发布的Array:



类似于:

  $ optionsInput = Input :: get('option'); 

if(is_array($ optionsInput)){
//处理您的选项,例如像这样
foreach($ optionsInput as $ key => $ input){
$ proceededOptionsArray [$ key] = $ input;






数组输入的相同规则适用于验证器,然后:

  //用其他`Input`返回

return Redirect :: back )
- > withErrors($ proceededValidatorArray + $ validator)
- > withInput($ proceededOptionsArray + Input :: all());


I have this form

@extends('index')

@section('main')
    <h1>Create a new poll</h1>
    {{ Form::model(new Poll, ['route' => 'polls.store', 'class' => 'create-poll']) }}
        <div class="gray-box">
            {{ Form::label("topic", "Write your poll question") }}
            {{ Form::text('topic', '', ['placeholder' => 'Example: What is the best number?', 'id' => 'topic']) }}
        </div>

        <div class="gray-box">
            {{ Form::label(null, "Write the possible answers") }}
            {{ Form::text('option[]', null, ['class' => 'option', 'placeholder' => 'Test option']) }}

            <input type="button" class="more-options" value="Add another option">
            {{-- This will create another option[] input element --}}

        </div>

        {{ Form::submit() }}
    {{ Form::close() }}
@stop

There is more to it, but that is the important part. Basically it is a poll name. It has a topic and at least one option. Clicking on a button, you can add more options.

This is the controller:

public function store() {
    $data = Input::all();

    // Validate input
    $validator = Validator::make($data, Poll::$rules);
    if ($validator->fails())
        return Redirect::back()->withErrors($validator)->withInput();

    ...

The problem here is that the WithInput() throws an error:

ErrorException

htmlentities() expects parameter 1 to be string, array given (View: /home/dbugger/laravelproject/app/views/polls/create.blade.php)

I suspect is because I am using an Array Form Element, but im not sure why or how, since at the time being I am not even trying to re-fill the form with the (failed) submitted data...

解决方案

For grouped inputs like Form::text('option[]')... or Form::checkbox('options[]')... you need to rearrange the posted Array in your controller:

Something like:

$optionsInput = Input::get('option');

if(is_array($optionsInput)) {
   // process your options, eg like this
   foreach($optionsInput as $key => $input) {
        $proceededOptionsArray[$key] = $input;
   }
}

The same rule for array inputs is applicable for the validator, then:

// return it with other `Input`

return Redirect::back()
    ->withErrors($proceededValidatorArray + $validator)
    ->withInput($proceededOptionsArray + Input::all());

这篇关于Laravel 4 - 发送失败提交时的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆