mercredi 18 avril 2018

Laravel 5: Method [validateTime] does not exist

When I'm trying to update my project and doing a POST request to my ProjectsController I get the following error:

Method [validateTime] does not exist

At first I was using a custom request, but after some researching I took to making a Validator in the controller, resulting in the following code in my Controller Update function:

   /**
     * @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!');
    }
}

However I still get the same error and have found no possible solution. I'm using the Validator class via use Illuminate\Support\Facades\Validator;. Below are my form and route, should they be of any help.

edit.blade.php:

<form action="" class="data-form" method="POST">
                
                <label for="name" class="data-label">Name</label>
                <input id="holder" type="text" class="data-input" name="name" value="" data-id="">
                <label for="desc" class="data-label">Description</label>
                <textarea class="data-textarea" name="desc" placeholder="Type your description here"></textarea>
                <label for="date" class="data-label">Deadline</label>
                <div class="data-input">
                    <input type="date" class="data-date" name="date" value="">
                    <input type="time" class="data-time" name="time" value="">
                </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"></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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire