【问题标题】:Laravel form not getting any data on redirect (no errors no input)Laravel 表单没有获得任何重定向数据(没有错误没有输入)
【发布时间】:2014-02-05 17:07:51
【问题描述】:

我有一个创建组织的表格。如果我没有通过组织的名称,它应该没有通过验证。在 store 方法中,我可以在 $this->validator->getErrors() 中看到正确的错误,然后将它们传递进去,但表单中没有显示任何内容。我可以从视图表单中转储错误和 Input:old() 但什么都没有。我错过了什么?

public function create()
{
    $supportedStates = ['' => 'Choose'] + $this->us_states->supportedStates();
    $procedures = $this->procedures->getList();
    $phoneTypes = $this->phone_types->lists('phone_number_type', 'id');
    return View::make('organizations.create', array('supportedStates' => $supportedStates, 'procedures' => $procedures, 'phoneTypes' => $phoneTypes, 'input' => Input::old()));
}

public function store()
{
    $input = Input::all();
    if($this->validator->passes())
    {
        $new_organization = $this->repository->create(['organization_name' => $input['organization_name']]);  
        if($input['logo_url'])
        {
            $new_organization->processImage($input, Request::root());             
        }
        $new_organization->createRelated($input);
        return Redirect::route('/')
            ->with('message', 'Organization Created.');
    }

    return Redirect::route('organizations.create')
        ->withInput()
        ->withErrors($this->validator->getErrors())
        ->with('message', 'There were validation errors.');
}

【问题讨论】:

  • 表单模板在哪里?
  • 表单模板在组织视图中
  • 我正在从表单视图中转储变量,但没有显示旧输入或错误($errors 为空)。

标签: php validation laravel-4


【解决方案1】:

您需要向我们展示您是如何在表单上显示错误的。 您是否使用以下内容来获取错误

错误在消息中。

//send this to your view from controller
$messages = $validator->messages();

//retrieve errors in view
foreach ($messages as $message)
{
    //
}

【讨论】:

  • 如果我在验证失败后返回重定向之前染色和转储,我会得到以下 $this->validator->getErrors() "object(Illuminate\Support\MessageBag)#619 ( 2) { ["messages":protected]=> array(1) { ["organization_name"]=> array(1) { [0]=> string(40) "组织名称字段为必填项。" } } [ "format":protected]=> string(8) ":message" } "。如果我尝试 Session::all() 我得到 array(1) { ["_token"]=> string(40) "dkAdwXBo5uvsaSGo5mhLL3RJmjyEUT21WNm3pvsG" } Session::get('error') 返回 NULL。
  • 很明显错误在消息中。 $messages = $validator->messages();foreach ($messages->all() as $message) { // } 见上面我编辑的答案
  • 不,我在寻找 $errors 或 $input。 $messages 未设置为变量。
  • 是的,我建议您可以通过将 $messages 或 $errors 分配给 $validator->messages() 来将其设置为变量。另外,您如何显示旧输入: {{Input::old('input_name')}} ?是 organizations.create 路由名称吗?
  • 据我了解 L4,当我使用 withErrors 或 withInput 重定向时,会将 $errors 和 $input 发送到重定向视图。是的, organizations.create 是一个路由名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2011-06-22
  • 2011-05-11
相关资源
最近更新 更多