New in Livewire 3: Validation with PHP Attributes and Form Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today we'll continue exploring Live Wire 3 and we will talk about validation and form objects in this video two things to enhance our simple form from a previous video where I demonstrated the live behavior in case you missed that video I will link that in the description below there was a quick demonstration of what Livewire is now in this video we'll talk about functions specifically for the new Live Wire 3. in Livewire 2 the syntax was different or form objects didn't even exist in Livewire 2. so in this video I will not even mention Livewire 2 syntax will go with the newest syntax so our goal is to have validation for the form Fields so whenever we click save if this input is not valid the validation rule should be underneath again without refreshing the page and without writing any JavaScript so this is basically two rules or two benefits while using Livewire Dynamic elements on the page without JavaScript so here's our Live Wire component from a previous video and when hit and save one of the ways to use validation actually I will show you three ways first way is just call this validate and then you provide the array of rules per field so you do title rule is for example required same as laravel all the laravel rules are here so required minimum three and body for example minimum 10 symbols and I remind you the title and Body in the Live Wire blade is wire model wire model title and wire model body now if refresh our page and don't fill anything we have validation errors of required and again no page refresh only this part is refreshed and then if we have something but too short the validation rule would be different so this is how easy it is to add validation in Live Wire component but this is just one syntax y let's explore the others the problem with this way a little problem in convenience that it is repeating so title and body here title and body here and also we introduce them as properties here so another way is to not call this validate with parameters but instead provide validation rules as attributes of those properties PHP attributes PHP attributes appeared in PHP language with php8 and I will link in the description below the article by brand from Stitcher where he explains what attributes are basically it's this thing so we've been using comments and dog blocks above the properties for years and years and from php8 there's a structured way now so live wire uses that structured way for providing attributes for these properties and one of the attributes is Rule so you can do hash Rule and then what are the rules for example required and minimum of three like this to use that you have to add the rule from Live Wire attributes here on top in my phpstorm auto completed similarly you pass the rule to body for example minimum 10 here and then you don't need to pass those validation rules you still need to call this validate but you don't have to call any parameters so if we refresh let's try it out save and the validation still works it's just different syntax the goal here is you define the property and Define the rules in the same place as property so if your component code is longer you don't need to scroll up and down to find out validation rules and then there's the third syntax and this is where we get introduced to form objects a new function in Live Wire three so basically you can offload all of that into a separate class similarly how in laravel you have form requests in live where you have form objects so in the terminal we'll run PHP Artisan Livewire form for example let's call it post form it generates this file and if we open that post form what we need to do is move that logic from the Live Wire component to the form object so in Live Wire component we don't need to have those properties we cut from here and we paste those as part of the form object then in the create post component we don't need to have that validate and save instead we create a separate function of store for example in here public function store we paste that here and then in the save we just call this form store for now we don't have this form we need to Define it as a property of the main component so public post form form like this so what we're doing here is kind of similarly how we're shortening the controllers in laravel to offload the logic somewhere else into like actions or services or form requests or others in Livewire attempt to do such a floating is form objects and also benefit of form objects that you can reuse the same form object like this in the component of create object and in component of edit object you just need to pass the parameter of the current record the final thing we need to do to make it work is to change wire model from title because there's no title anymore on our component we need to bind that as form dot title and form dot body and also here the errors would be with form title and form body here the validation errors now if you refresh our component we hit save validation is working and if we use fake filler to fill in the form and yeah I forgot one thing we don't reset the title and the body here instead again we move that to the form object and we just do this reset without any parameters basically resetting all try again fake filler save and now it works so in this video you saw three concept of Live Wire validation in a simple way then validation via attributes of PHP and also form objects and this demonstration comes from my course about Livewire 3 which I published this week so you can see the same component create post with form validation and other functions so the list of lessons is pretty huge 30 lessons and in tomorrow's video for one more day I will demonstrate another thing of Live Wire which is live updates before submitting the form and live validation as someone is typing in the form so this is the plan for tomorrow but if you want the full course I will link that as well in the description below that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 7,133
Rating: undefined out of 5
Keywords:
Id: 40HnNOGilGg
Channel Id: undefined
Length: 7min 1sec (421 seconds)
Published: Wed Aug 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.