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

jurandou's avatar

Test field unicity with formRequest on function boot

Hi,

I'm trying to generate a slug with my Post title, using boot model function with static::creating. It works fine while it's unique. But if this duplicates existing slug, i cant' achieve to pass the test of my NewsFormRequest in the store resource method, and i'm getting the Integrity constraint violation: 1062. I guess i'm missing something logic, don't know what...

Thanks in advance ! Julien

0 likes
8 replies
tykus's avatar

There are sluggable packages on Packagist that can do this for you, but if you wish to roll your own, have a look at the code used in the Forum series, and the discussion around different gotchas here

jurandou's avatar

Thanks for replying tykus,

But my own sluggable method works fine, the issue is about passing the Request test of my own FormRequest in the controller. The slug field unicity is not tested, and instead of sending the flash session message, it create the post.

I guess the boot function is executed after my FormRequestTest...

tykus's avatar

Of course a FormRequest would be executed before the model is saved.

What exactly does your test and controller method look like?

jurandou's avatar

My store method :

public function store(NewsEditFormRequest $request)
    {

      Post::create([
        'title'           =>  $request->title,
        'body'            =>  $request->body,
        'image'           =>  $request->image,
        'published'       =>  $publish,
        'published_home'  =>  $publishHome,
      ]);

      return redirect()->route('news')
                    ->with('success', true)
                    ->with('message', "L'actualité " . $request->title . " a bien été créée");
    }

My FormRequest (NewsFormRequest):


public function rules()
    {
        return [
            'title' =>  'required|max:60',
            'slug'  =>  'unique:posts',
            'body'  =>  'required',
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'title.required'  =>  'Le champ Titre est obligatoire',
            'title.max'       =>  'Le champ Titre ne doit pas dépasser les 60 caractères',
            'slug.unique'     =>  'Le titre doit être unique, celui-ci existe déjà',
                        'body.required'   =>  'Le champ Contenu est obligatoire',
        ];
    }

My Post Model:


protected $fillable = ['title', 'body', 'image', 'published', 'published_home'];

protected static function boot()
    {
      parent::boot();

      static::creating(function($news) {
        $news->slug = str_slug($news->title);
      });
    }

tykus's avatar

Where is the FormRequest test; what are you testing?

Your FormRequest is executed before anything inside the Controller method - only if authorization and validation pass will the controller method be executed. Since the Post::create([...]) method is called in the controller method, the creating event is fired after the FormRequest has done its work.

jurandou's avatar

I think there is something i don't understand. I'm trying to use the $news->slug of the boot function in the NewsEditFormRequest to test the slug field unicity. It seems I miss to pass the variable in my FormRequest...

tykus's avatar
tykus
Best Answer
Level 104

Your FormRequest should be validating only the form input that came from the user; your user is not sending the slug, it is being determined by your app.

So, you do not need to validate a slug, because the slug is not in the Request object

jurandou's avatar

While the slug is being generated by the title, i'll test the unicity of the title, i don't find another way to do it...

Thank you tykus

Please or to participate in this conversation.