php - laravel 4 Input::old() 为空

标签 php input laravel-4

我在一个项目中使用 Laravel 4,但遇到了问题。我不确定,我做错了什么

详细信息:

  • 表单被发送到 Controller 的保存函数。
  • 当验证失败时,我将重定向到创建函数
  • 重定向后(使用 Redirect::to(somewhere)->withErrors($validator)->withInput()):
    • 正确显示验证错误(如果有)
    • Input::old() 为空(它应该包含之前提交的数据)

在 Controller 中创建函数

public function create()
{
    $this->scripts[] = 'various js path here';

    return View::make('admin.modules.events.create', array(
        // Loading various scripts specified in this function
        'scripts' => $this->scripts,
    ));
}

在 View 中:

...
{{ Form::bsInput('event_name', 'Event title', 'event title goes here', $error = (($errors->has('event_name')) ? $errors->get('event_name') : false), $type = 'text', Input::old('event_name')) }}
...

注意:bsInput 是 Form::Input() 的包装器,用于创建引导控件和标签

Controller :

public function save()
{

    if (Input::has('submitEventSave'))
    {
        $event = Mihirevent::find(Input::get(event_id));
        $event_add = false;
    }
    else
    {
        $event = new Mihirevent();
        $event_add = true;
    }

    if ($event === false)
    {
        // doing something else
    }
    else
    {

        $event->event_name              = Input::get('event_name');
        $event->event_slug              = Input::get('event_slug');
        $event->event_description       = Input::get('event_description');
        $event->event_location_text     = Input::get('event_location_text');
        $event->event_location_data     = Input::get('event_location_data');
        $event->event_status            = Input::get('event_status');
        $event->featured_image          = Input::get('featured_image');
        $event->event_date_from         = Input::get('event_date_from');
        $event->event_date_until        = Input::get('event_date_until');

        $validation_rules = $event_add === true?$event->rules:$event->update_rules;

        $inputs = array(
            'event_name'            => $event->event_name,
            'event_slug'            => $event->event_slug,
            'event_location_text'   => $event->event_location_text,
        );


        $validator = Validator::make($inputs, $validation_rules);

        if ($validator->fails())
        {
            Input::flash();
            if ($event_add === true)
            {
                return Redirect::to('admin/event/create')
                           ->withErrors($validator)->withInput();

            }
            else
            {
                return Redirect::to('admin/event/edit/'.$event->event_id)
                            ->withErrors($validator)->withInput();
            }
        }

        // save
        MihirEvent::save();

        // redirect to list 
        return Redirect::route('adminEvent');

    }
}

更新:

bsInput 宏:

Form::macro('bsInput', function($name, $text, $placeholder = null, $error = false, $type = 'text', $default = null, $class=null)
{
    $label = Form::label($name, $text, array('class' => 'control-label'));
    $input = Form::input($type, $name, $default, array('placeholder' => $placeholder, 'class' => 'form-control'.($class?' '.$class:'')));

    $error_messages = false;
    if($error)
    {
        $error_messages = '<ol>';
        foreach ($error as $value) {
            $error_messages .= '<li>'.$value.'</li>';
        }
        $error_messages .= '</ol>';
    }

    $html  = '<div class="form-group'.(($error) ? ' has-error' : '').'">';
    $html .= $label;
    $html .= $input;
    $html .= (($error_messages) ? '<div class="alert alert-danger">'.$error_messages.'</div>' : '');
    $html .= '</div>';

    return $html;
});

最佳答案

查看 Laravel 4 源代码:

/**
 * Flash an array of input to the session.
 *
 * @param  array  $input
 * @return \Illuminate\Http\RedirectResponse
 */
public function withInput(array $input = null)
{
    $input = $input ?: $this->request->input();

    $this->session->flashInput($input);

    return $this;
}

看起来如果您不使用 ->withInput 传递一个输入数组,它会尝试从原始请求中提取它。尝试像这样修改行:

        if ($event_add === true)
        {
            return Redirect::to('admin/event/create')
                       ->withErrors($validator)->withInput(Input::all());

        }
        else
        {
            return Redirect::to('admin/event/edit/'.$event->event_id)
                        ->withErrors($validator)->withInput(Input::all());
        }

这应该有希望强制它传递输入值数组,而不是依赖

        '$this->request->input()'

仍然存在于 session 中。

关于php - laravel 4 Input::old() 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23063916/

相关文章:

php - 如何有效地只选择 Eloquent 中属于多的关系的 ID?

javascript - SymPy 在 1&1 托管主机上安装

php - 警告 : preg_match() [function. 预匹配]:未知修饰符 '('

php - Paypal按钮集成问题

c++ - 当找不到分隔符时,如何阻止 cin.getline() 导致控制台重复获取用户输入?

javascript - 选中单选按钮时显示 div

php - 如何在 Laravel 4 中使用没有 id 的 Eloquent 更新数据

php - 如何从laravel中的mysql存储过程中获取多个结果集

php - 上传视频,使用 PHP 服务器端格式化和编码

mysql - 使用 MySQL 触发器替换输入