Laravel报错Method Illuminate\Validation\Validator::validate{ouuowwd的解决
解决Laravel提交表单时
Method Illuminate\Validation\Validator::validate{ouuowwd}错误 提交表单时触发Method Illuminate\Validation\Validator::validate{ouuowwd}错误,核心原因是表单验证规则的语法错误——你把验证规则的分隔符|写在了字符串外部,导致Laravel无法正确解析规则,触发了无效规则的报错。
看你代码里的验证规则部分,比如:
'bedroom1' => 'image'|'nullable', 'address' => 'string'|'required',
这种写法是错误的,|是Laravel验证规则的分隔符,必须放在同一个字符串内部,用来分隔多个验证规则。
修复后的代码
把所有拆分到多个字符串的验证规则合并成单个字符串,用|分隔规则:
namespace App\Http\Controllers; // 注意:需要引入对应模型和Request类 use Illuminate\Http\Request; use App\Models\State; use App\Models\Area; use App\Models\Property; class PropertyController extends Controller { public function index() { // } public function create() { return view('pages.uploadapartment', [ 'states' => State::all(), 'areas' => Area::all(), ]); } public function store(Request $request) { $validatedData = $request->validate([ 'building' => 'required', 'buildingtype' => 'required', 'flatbuildingtype' => 'nullable', 'buildingincompound' => 'required', 'firstfloor' => 'nullable', 'secondfloor'=> 'nullable', 'thirdfloor' => 'nullable', 'bedroom1' => 'image|nullable', 'kitchenpic' => 'image|nullable', 'bedroom2' => 'image|nullable', 'bedroom3' => 'image|nullable', 'bedroom4' => 'image|nullable', 'address' => 'string|required', 'area' => 'string|required', 'state'=> 'string|required', 'meetingplace'=> 'string|required' ]); $apartment = Property::create([ 'building' => $request->building, 'FlatType' => $request->flatbuildingtype, 'firstfloor' => $request->firstfloor, 'secondfloor'=> $request->secondfloor, 'room3' => $request->bedroom3, 'kitchenpic' => $request->kitchenpic, 'compound' => $request->bedroom4, ]); // 注:create方法会自动保存数据,无需重复调用save() // $apartment->save(); return redirect('/')->with('status', 'Your apartment for rent was successfully uploaded.'); } }
额外提示
create方法调用后无需再执行save(),因为create已经会将数据写入数据库;- 确保已经正确引入
Request、State、Area、Property这些类,避免出现类找不到的错误。
内容的提问来源于stack exchange,提问作者Stephen
相关产品推荐
相关产品推荐

