Okay, so today I messed around with “lavar.” It all started because I needed a quick way to validate some forms, and I remembered hearing about this thing. I was like, “Let’s give it a shot!”
Getting Started
First things first, I needed to, you know, actually get it. I used Composer because, well, that’s how I roll these days. I opened up my trusty terminal and typed something like:
I hit enter, and watched the magic happen. A bunch of text scrolled by, and boom, it was installed. I didn’t really check what version or anything. I just assumed it would grab the latest one.
Figuring it Out
Next up, I needed to actually use it. The thing is, I didn’t really read any documentation. I kinda just jumped in, hoping it would be intuitive. I created a simple array with some data, like:
$data = [
'name' => 'Bob',
'email' => 'bob@example' // missing .com!
See, I made a mistake with the email, right? Let see if it will work.
Testing
Then, I made a rules array, like:
$rules = [
'name' => ['required', 'string'],
'email' => ['required', 'email']
Basic stuff. Name is required and a string. Email is required and, well, an email.
Putting it Together
After that, I tried to validate it. I think I did something like this:
use TonysmLavarLavar;
if (Lavar::fails($data, $rules)) {
$errors = Lavar::errors($data, $rules);
//print_r to display
print_r($errors);
} else{
echo "Valid Data";
The Results
And guess what? It worked! It caught the email error, just like I wanted. It gave me back some kind of error message, which I could then display to the user. I didn’t spend much time making it look pretty. I just wanted to see if it worked, and it did.
The error message shows something like this:
Array
[email] => Array
[0] => The email must be a valid email address.
Wrapping Up
So yeah, that’s my “lavar” adventure. It was pretty straightforward. I grabbed it, threw some data at it, and it told me what was wrong. I’m sure there’s a lot more to it, but for a quick and dirty validation, it seemed to do the trick. I might actually use this in a real project sometime!