【问题标题】:Laravel 5: Method [validateTime] does not existLaravel 5:方法 [validateTime] 不存在
【发布时间】:2018-09-28 09:48:27
【问题描述】:

当我尝试更新我的项目并向我的 ProjectsController 发出 POST 请求时,我收到以下错误:

方法 [validateTime] 不存在

起初我使用的是自定义请求,但经过一些研究后,我开始在控制器中制作验证器,从而在我的控制器更新函数中生成以下代码:

   /**
     * @param Req $request
     * @param \App\Project $project
     *
     * @return string
     */
    public function update(Req $request, Project $project)
    {
        if ( ! Request::ajax()) {

        $sTitle = implode(' '.Config::get('properties.title_separator').' ', [
            $project->name,
            __('Edit')
        ]);
        $sHeader = $project->name . ' ' . __('Settings');

        $deadline = new DateTime($project->deadline);

        if ($request->isMethod('POST')) {

            $aRules = [
                'name'          => 'string|required',
                'desc'          => 'string',
                'date'          => 'date|required',
                'time'          => 'time|required',
                'status'        => 'string|required',
                'priority'      => 'string|required'
            ];
            $aMessages = [
                'name.required'     => 'If you can\'t name your project, at least name it Voldemort or something',
                'name.string'       => 'A textual value would be appreciated kthxbye',
                'desc.string'       => 'Don\'t worry, PCs understand Latin script, you don\'t have to write in binary!',
                'date.required'     => 'Everyone who\'s working on this project is stuck on it for all eternity..! Give it a deadline m8',
                'date.date'         => 'Time is the one thing that outlives all, if it\'s a valid date that is..',
                'time.required'     => 'Dude if there\'s no specific time, just add 00:00',
                'time.time'         => 'No, putting "beer\'o\'clock" in here is not going to work.',
                'status.required'   => 'If nobody\'s working on a project, means it\'s "On hold"! At least put that as a status',
                'priority'          => 'Should we even care? A sense of priority might be nice'
            ];

            $oValidator = Validator::make($request->all(), $aRules, $aMessages);

            if ($oValidator->fails()) {
                $aErrors = $oValidator->errors();
            }

            if (count($aErrors) == 0) {
                $project->name          = $request->input('name');
                $project->description   = $request->input('desc');
                $project->deadline      = DateTime::createFromFormat(
                    'Y-m-d H:i:s',
                    $request->input('date') . ' '. $request->input('time')
                );
                $project->status        = $request->input('status');
                $project->priority      = $request->input('priority');

                $project->save();


                $this->redirectWithAlert(
                    'success',
                    __FUNCTION__,
                    'project',
                    $project->name,
                    'projects/'.$project->id
                );
            } else {
                return view(
                    $this->prefix.'edit',
                    compact(
                        'sTitle',
                        'sHeader',
                        'project',
                        'deadline'
                    )
                )->withErrors($aErrors);
            }
        }

    } else {
        // Check if item is defined
        if ($_POST['id'] && $_POST['status']) {

            // Find project with defined id
            $oProject = Project::find($_POST['id']);

            // Get Authenticated user and adhering projectRole
            $oLoggedInUser = Auth::user();
            $oProjectRole = $oLoggedInUser->projectRole()->where('project_id', $oProject->id)->first();

            // If project is found, change and save it's status
            if($oProject && $oProjectRole->name == 'Projectbeheerder') {
                switch ($_POST['status']) {
                    case 'inprogress':
                        $oProject->status = 'in progress';
                        break;
                    case 'onhold':
                        $oProject->status = 'on hold';
                        break;
                    case 'complete':
                        $oProject->status = 'complete';
                        break;
                }

                $oProject->save();
                return json_encode('Project\'s status changed!');
            }
            return json_encode('Couldn\'t find project or not authorized!');
        }

        return json_encode('Project not specified!');
    }
}

但是我仍然遇到同样的错误并且没有找到可能的解决方案。我通过use Illuminate\Support\Facades\Validator; 使用Validator 类。 以下是我的表格和路线,如果它们有帮助的话。

edit.blade.php:

<form action="{{ url('projects/' . $project->id . '/update') }}" class="data-form" method="POST">
                {{ csrf_field() }}
                <label for="name" class="data-label">Name</label>
                <input id="holder" type="text" class="data-input" name="name" value="{{ $project->name }}" data-id="{{ $project->id }}">
                <label for="desc" class="data-label">Description</label>
                <textarea class="data-textarea" name="desc" placeholder="Type your description here">{{ $project->description }}</textarea>
                <label for="date" class="data-label">Deadline</label>
                <div class="data-input">
                    <input type="date" class="data-date" name="date" value="{{ $deadline->format('d - m - Y') }}">
                    <input type="time" class="data-time" name="time" value="{{ $deadline->format('HH:mm') }}">
                </div>
                <label for="status" class="data-label">Status</label>
                <select name="status" id="" class="data-select">
                    <option value="on hold">On hold</option>
                    <option value="in progress">In progress</option>
                    <option value="complete">Complete</option>
                </select>
                <label for="priority" class="data-label">Priority</label>
                <select name="priority" class="data-select">
                    <option value="low">Low</option>
                    <option value="normal">Normal</option>
                    <option value="high">High</option>
                    <option value="highest">Highest</option>
                </select>
                <p class="data-label">Project creator</p>
                <span class="data-span">{{ $project->madeBy()->nameFormatter() }}</span>
                <input type="submit" class="data-submit" name="submit" value="Submit changes">
            </form>

web.php:

Route::post(
                    'update',
                    ['as' => 'projects.edit', 'uses' => 'ProjectsController@update']
                ); // with prefix this becomes projects/{project}/update

【问题讨论】:

  • 没有time验证
  • Ben's answer 有关于解决方案的信息。如果您觉得他的回答对您有帮助,请采纳。

标签: php laravel validation laravel-5


【解决方案1】:

使用date_format:"Y-m-d" 规则验证仅日期字段,使用date_format:"H:i:s" 规则验证仅时间字段。

date 使用 php 函数 strtotime 并且是宽松的验证规则,它都匹配 date Y-m-d, time H:i:s, datetime Y-m-d H:i:s ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多