PrepareForValidation, generate data before validating forms in Laravel
Usually in applications that handle a field for the slug, it is generated internally by another field, in this case, the title field; in Laravel, we can easily do this process of generating the slug automatically using the prepareForValidation() function in which we merge with the slug generated from the title using the slug() helper provided by Laravel; remember that the prepareForValidation() function is executed before performing the validations; so, we place in both validation files:
app/Http/Requests/Category/Store.php
app/Http/Requests/Category/Put.php
// ***
public function prepareForValidation()
{
if(str($this->slug)->trim() == "")
$this->merge([
'slug' => str($this->title)->slug()
]);
}
// ***
Transcripción del vídeo
We are going to make a small optimization for our form and with this we literally take 50% of the time that the user is going to spend working with our form, which would be to auto-generate the slot. Since in 99.9999% of the cases we are going to want the slot to be the same as the title but with its corresponding syntax, that is, to lowercase and the spaces separated by hyphens and removing the special characters. So we can do that very easily in Laravel by converting it into a stream and already there in the help functions we have a method called slug that allows us to do exactly that job, so for that we have here a very interesting method called prepare for validation which does exactly what its name indicates, it is going to be executed before, that is, it is preparing the Data for the validations, therefore, just as its name indicates, it is going to be executed before what would be the validations, which is what we have in the controller, that is, it is a kind of middleware that is in charge of preparing the Data, for the rest here it would be a bit of the syntax, here we ask if it is empty, therefore the user does not He provided it and in that case is when we are going to edit it… we do a merge here as you can see, that is to say we are mixing what we currently have, remember that the merch refers to mixing, we mix the request that we already have with the changes that we are making here and well, you can take advantage of this to do any other type of operation in case it is necessary for you.
We are going to expand it here, I say here, for God's sake, here. So this is the basic one again, this has nothing to do with inertia, but again we can use it without problems, let's see if it comes out, I think it doesn't come out, I'm going to write it here in the same way, prepare and no. Well, here, until the self-help arrived, I'm going to prepare for validation again, I'm going to remove this and here we do a little of what was mentioned… so for that we can use the trim method.
It's marking there So I put it here inside is that, it happens to me through the zoom is it that here outside it had to appear No neither Well I don't know okay Here it is and here we place the slot and we do the magic we place here it is again because we are going to convert this again a str object from laravel we pass it to the title important is the title the loot don't get confused since the title if it were full Or it has to be full and here we use the function or method here it would be the des slot method that does what was commented here I can also search for it since it is open here it is down here and what it does is generates a friendly or clean URL as I call it as I call it for a field called salut Ah well here you can also indicate what the character will be that by default is the hyphen for the spaces, that is to say this space that is here I place it And that would be it Practically everything here now we need to test if our optimization is working correctly I think I'm going to reload here I'm going to remove this here it is already the same so I'm going to place one here I give edit and this blew up okay let's see what happened here put merch does not exist I put merch merch to see merch now yes Okay here Remember that there is nothing I'm going to reload in the same way I remove this and I'll edit the URL has been taken m I think it's trying to change to the same URL it's funny it didn't occur to me but even so it shouldn't change Well I'm going to another example like this one let's see I'm going to remove this one I'll edit and here I will generate it Okay here I'm going to try to put something more interesting Okay here notice important if here we make a change in the title we usually don't want to change the slug and that is why the verification that we did before with the conditional and Okay Okay I think that we do have a problem ... with the slug Ok in the end I was checking here with the sl to see Ok Ok Ok it would be the sl I think that the ID is better to place the ID that would be the one that we do not change if we definitely place the ID there because if we place the how we are changing it seems that if it gives a problem anyway Okay here if I remove it there it does appear Okay I have to reload the page for the loot to appear And well you can see how Here I also placed this There it is and it was changed here it will not change the slot it keeps it which is usually what we want so that the slug is not changed when we change the title for reasons of desire since this is supposed to be some kind of Block So that is basically it here already the reload part for it to appear here it would really have to be analyzed well Well we are going to leave this like this because when we do the redirection this is easily solved right now it is because we are doing the operation here almost already but we will fix this a little later so well let's go to the next class.
- Andrés Cruz
This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro Laravel 11 con Tailwind Vue 3, introducción a Jetstream Livewire e Inerta desde cero - 2024.
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter