r-u-herrmann's avatar

Livewire validation

I have a Field "trip_number" which can be numeric or van contain a string "Leerfahrt" or "Vorladen" The trip_number is always required.

itried this code which doesnt work:

0 likes
4 replies
argethink's avatar

Livewire validation

    public $trip_number= '';

    public function save()
    {
        $validated = $this->validate([ 
            'trip_number' => 'required',
        ]);

 		//  You can do whatever you want.
        Post::create($validated);
        return redirect()->to('/posts');
    }
tykus's avatar

@argethink something like this:

    $validated = $request->validate([ 
        'trip_number' => [
            'required',
            function ($attribute, $value, $fail) {
               if (!(is_numeric($value) || in_array(strtolower($value), ["leerfahrt", "vorladen"]))) {
                    $fail("The {$attribute} field is not valid");
               }
            }
        ],
    ]);

Please or to participate in this conversation.