Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BrownieCoffee's avatar

My second button is not recognized in my form

Hi there,

I have problem with my form.

There are two buttons. One for draft, the other for publish

        <form action="{{route('projects.updateDraft', [$project, $slug])}}" method="POST" enctype="multipart/form-data">
...

          <!-- *  Footer * -->

            <div class="project-form__sub-footer">
                <button class="project-form__button button is-rounded" type="submit" name="submit" value="draft" style="margin-bottom: 1rem;">
                    <span>ajouter au brouillon</span>
                </button>
                <button class="project-form__button project-form__button--submit button is-rounded"  type="submit" name="submit" value="publish">
                    <span>poster mon projet !</span>
                </button>
            </div>
</form>

Before this worked but I don't know why not now.

there are that in my controller


    /**
     * Update the drafted project in database
     *
     * @param  \Illuminate\Http\Requests\EditProject  $request
     * @param int $project | id of the project
     * @param string $slug | slug of the project
     * @param string $token | token of the drafted project
     * @param  \Services\ProjectService $projectService
     * @return \Illuminate\Http\Response
     */
    public function updateDraft(EditProject $request, $project, $slug, ProjectService $projectService)
    {

...


  if ($request->submit == 'draft') {
            //si le status du projet est en publié
            if ($project->status_id == 2) {
                //je le change en brouillon
                $project->update([
                    'status_id' => 1
                ]);
            }
            //je retourne à la dernière page
            return back()->with('status', 'Projet sauvé au brouillon ! Vous pourrez le publier en cliquant sur le bouton de publication.');
        }
        //si l'utilisateur clique sur le bouton de publication
        if ($request->submit == 'publish') {
            //j'édite le status du projet en publié
            $project->update([
                'status_id' => 2
            ]);

            //je redirige l'utilisateur vers le projet qu'il vient de publié
            return redirect()->route('projects.show', [
                'project' => $project,
                'slug' => $slug
            ])->with('status', 'votre projet vient est à présent publié !');
        }

web.php

  Route::get('/projets/{project}/{slug}/modifier-mon-brouillon/{token}', 'ProjectController@draft')->name('draft');

     
        Route::patch('/projets/{project}/{slug}/modifier-mon-brouillon', 'ProjectController@updateDraft')->name('updateDraft');

only the draft button works but publish button fails.

Can you help me please?

Thank by advance. See you.

EDIT: When I check to the network in chrome developpers tools, chrome knows what button is clicked....

I really don't understand why my form is not send so...

0 likes
13 replies
automica's avatar

@browniecoffee when you say 'publish button fails' how do you mean? the form doesn't submit or it submits as a draft?

BrownieCoffee's avatar

@automica when I click on the published button, it's like I redirect on the form page without any changing. Only my draft button works.

automica's avatar

@browniecoffee so its posting to your method but not changing your status?

If you dd the request->submit are you getting 'publish' or 'draft' if you press your publish button?

automica's avatar

@browniecoffee is there anything in the EditProject requestHandler modifying your $request ?

Also if you dd when you press draft, do you see anything relating to that?

Snapey's avatar

Does it call this controller function at all? Perhaps you are hitting the create route and not this one.

BrownieCoffee's avatar

@snapey @automica All my route are writen after checking.

Route::get('/projets/{project}/{slug}/modifier-mon-brouillon/{token}', 'ProjectController@draft')->name('draft');

     
        Route::patch('/projets/{project}/{slug}/modifier-mon-brouillon', 'ProjectController@updateDraft')->name('updateDraft');

@automica talked about my EditRequest.

I replace public function updateDraft(EditRequest $request) by public function updateDraft(Request $request) to test.

Now my dd($request->submit === 'publish) works.

But I still don't know what is the problem with the EditProject request :/

<?php

namespace App\Http\Requests;

use App\Project;
use Illuminate\Foundation\Http\FormRequest;

class EditProject extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // je retrouve le projet
        $project = Project::find($this->project);

        if (request()->input('submit') == 'draft') {
            // les champs pourront être nullable
            $fieldValidation = 'nullable';
        } else {
            //     //autrement, ils seront requis
            $fieldValidation = 'required';
        }


        //le titre devra répondre aux critères si dessous, en prenant soin d'ignorer la vérification du titre unique pour ce même projet
        $project_material = $project->materials()->first();
        //si mon projet a des materiels
        if ($project_material !== null) {
            //je récupère le premier id du materiel que je viens stocker en variable
            $first_material = $project->materials()->first();

            // dd($project->materials()->first());
            $first_material_id = $first_material->id;
            $field = 'material.' . $first_material_id;
        } else {
            //s'il n'en a pas, le premier champs matériel sera le champs 0
            $field = 'material.0';
        }


        //si ce projet a une image thumbnail
        if ($project->thumbnail !== null) {
            $thumbnailValidation = 'sometimes|image|mimes:jpg,jpeg,png|max:3000';
        }



        return [
            'title'                             =>  "$fieldValidation|string|min:6|max:80|unique:projects,title,$project->id",
            'thumbnail'                         =>  $thumbnailValidation,
            'category'                          => "$fieldValidation|exists:categories,id",
            'difficulty'                        => "$fieldValidation|exists:difficulty_levels,id",
            'material.*'                        => 'distinct|nullable|string',
            $field                              => "$fieldValidation|string|min:3",
            'duration'                          => "$fieldValidation|integer|min:1|max:24",
            'unity_of_measurement'              => "$fieldValidation|exists:unities_of_measurement,id",
            'budget'                            => "$fieldValidation|integer|min:1|max:1000",
            'content'                           => 'required|min:100'
        ];
    }


    public function messages()
    {
        return [
            'title.required'        => 'Le titre doit être inscrit.',
            'title.string'          => 'Le titre doit être une courte phrase',
            'title.min'             => 'Le titre doit contenir au minimum :min caractères.',
            'title.max'             => 'Le titre doit contenir  au maximum :max caractères.',
            'title.unique'          => 'Il semblerait que ce titre soit déjà pris.',

            'thumbnail.required'    => 'Une image thumbnail doit être présent pour illustrer votre projet',
            'thumbnail.image'       => 'Votre fichier doit être une image',
            'thumbnail.mimes'       => "L'image thumbnail doit être de type .jpg, .jpeg, .png",
            'thumbnail.max'         => "L'image semble être trop lourde",
            'thumbnail.uploaded'    => "Il y a une erreur lors du téléchargement de l'image.",

            'category.required'     => 'Une catégorie doit être choisie pour votre projet.',

            'difficulty.required'   => 'Un niveau de difficulté doit être choisie pour votre projet.',

            'material.0.required'   => 'Au moins un matériel doit être renseigné.',
            'material.0.min'        => "Le matériel doit être composé d'au moins :min caractères.",

            'duration.required'     => 'Le temps estimé du projet doit être renseigné.',
            'duration.integer'      => "Ce champs n'accepte que des chiffres",
            'duration.min'          => 'La durée ne peut pas être inférieure à :min',
            'duration.max'          => 'La durée ne peut pas être supérieur à :max',

            'budget.required'       => 'Le budget du projet doit être renseignée.',
            'budget.integer'        => "Ce champs n'accepte que des chiffres",
            'budget.min'            => 'Le budget ne peut pas être inférieur à :min €',
            'budget.max'            => 'Le budget peut pas dépasser :max €',

            'content.required'      => 'Le contenu du projet ne peut pas être vide.',
            'content.min'           => 'Le contenu du projet doit contenir au moins :min caractères.',


        ];
    }
}


automica's avatar
automica
Best Answer
Level 54

@browniecoffee if comment out your validation rules, your EditProject should then allow you to save.

i'd then add these rules back in 1 by 1 and this should help your find the failing rule, or where its erroring.

BrownieCoffee's avatar

@automica @snapey After a break moment, I figured out my error

 'category'                          => "$fieldValidation|exists:categories,id",
            'difficulty' //here                       => "$fieldValidation|exists:difficulty_levels,id",

I replace difficulty by difficulty_level ( the name of my input)

Now It works. Thank you both .

Please or to participate in this conversation.