【发布时间】:2015-02-13 04:47:46
【问题描述】:
我遇到了一个问题,即我的编辑表单中的任何输入错误都返回“没有模型 [modelName] 的查询结果”,而不是像应有的那样生成验证器消息。这是我的代码:
宠物控制器.php
public function update($id)
{
//omited the validator messages cause it's too long, but it works cause I have an add function that uses the same thing.
try {
$pet = Pet::findOrFail(Input::get('id'));
}
catch(exception $e) {
return Redirect::to('/pet')->with('flash_message', 'Error Editing Pet.');
}
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$breed = filter_var($_POST["breed"], FILTER_SANITIZE_STRING);
$birthday = filter_var($_POST["birthday"], FILTER_SANITIZE_STRING);
$pet->name = $name;
$pet->breed = $breed;
$pet->birthday = Pet::saveDateFmt($birthday);
$pet->save();
return Redirect::to('/pet')->with('flash_message','Your changes have been saved.');
}
因此,任何名称、品种或日期的输入错误都会将我重定向到 /pet,并带有来自此函数的错误消息:
public function edit($id){
try {
$pet = Pet::findOrFail($id);
}
catch(exception $e) {
return Redirect::to('/pet')
->with('flash_message', $e->getMessage());
//this is where I get the error msg
}
return View::make('edit_pet', ['vet_list' => Vet::lists('name','id')])->with('pet', $pet);
}
我的意思是,我很高兴它能够捕捉输入错误,但我不想每次都将用户重定向回索引页面。我需要了解为什么它会以这种方式重定向以了解如何修复它。
【问题讨论】:
-
您在代码中使用
$validator->fails()或$validator->passes()吗?您能告诉我您的findOrFail()代码吗? -
findorfail() 代码来自 laravel。验证器适用于添加宠物的另一个功能。不过我没有使用失败/通过。
标签: php redirect laravel controller routes