Home > other >  Why does this code Fail once i add validation in laravel 8?
Why does this code Fail once i add validation in laravel 8?

Time:02-11

I am just starting to learn Laravel and I am a little lost here. This code works without validation.

The task is to build a website with user registration Posting data to an already existing database. As I have pointed, without validation it works just fine but when i add validation it fails.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Member;

class RegController extends Controller
{
    public function store(Request $request) {


        Member::create([

        'fname' => $request ->fname,
        'lname' => $request ->lname,
        'gender' => $request ->gender,
        'msisdn' => $request ->msisdn,
        'email' => $request ->email,
        'status' => $request ->mstatus,
        'no_of_children' => $request ->kids,
        'occupation' => $request ->profession,
        'age' => $request ->age,
        'bna' => $request ->bna,
        'residence' => $request ->residence,
        ]);
        
        return redirect('/members/register');
    
       }
}

But this doesn't...


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Member;

class RegController extends Controller
{
    public function store(Request $request) {

        $request->validate([
            'fname' => 'required',
            'lname' => 'required',
            'gender' => 'required',
            'msisdn' => 'required|unique:members_tbl',
            'email' => 'required|unique:members_tbl',
            'status' => 'required',
            'kids' => 'required',
            'profession' => 'required',
            'age' => 'required',
            'bna' => 'required',
            'residence' => 'required',
        ],
        [
            'fname.required' => 'Please Enter your First Name',
            'lname.required' => 'Please Enter your Last Name',
            'gender.required' => 'Please Enter your Gender',
            'msisdn.required' => 'Please Enter your Phone Number',
            'msisdn.unique' => 'A user has already registered with the phone Number',
            'email.unique' => 'A user has already registered with the email',
            'email.required' => 'Please enter a valid email address',
            'status.required' => 'Please Let us know your marital status',
            'kids.required' => 'Please fill out this field',
            'profession.required' => 'Please fill out this field',
            'age.required' => 'Please fill out this field',
            'bna.required' => 'Please fill out this field',
            'residence.required' => 'Please let us know where you live',
        ]);


        Member::create([

        'fname' => $request ->fname,
        'lname' => $request ->lname,
        'gender' => $request ->gender,
        'msisdn' => $request ->msisdn,
        'email' => $request ->email,
        'status' => $request ->mstatus,
        'no_of_children' => $request ->kids,
        'occupation' => $request ->profession,
        'age' => $request ->age,
        'bna' => $request ->bna,
        'residence' => $request ->residence,
        ]);
        
        return redirect('/members/register');
    
       }
}

What am i missing? The code works well without the validation but just redirects back to the form when validation is added.

Here is my Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Member extends Model
{
    use HasFactory;

    public $timestamps = false;

    protected $table="members_tbl";

    protected $fillable =[
        'fname',
        'lname',
        'gender',
        'msisdn',
        'email',
        'status',
        'no_of_children',
        'occupation',
        'age',
        'bna',
        'residence'
    ];
}

This is the result of var_dump($request)

 public 'request' => 
    object(Symfony\Component\HttpFoundation\InputBag)[44]
      protected 'parameters' => 
        array (size=13)
          '_token' => string 'DuNeITGaDF9K7eEMmU4HwX7nVLJUYDaZ1iYGT4dw' (length=40)
          'fname' => string 'Joe' (length=3)
          'lname' => string 'Peski' (length=5)
          'gender' => string 'male' (length=4)
          'msisdn' => string '254722000001' (length=12)
          'email' => string '[email protected]' (length=18)
          'residence' => string 'New Place' (length=9)
          'age' => string 'teen' (length=4)
          'bna' => string 'no' (length=2)
          'mstatus' => string 'single' (length=6)
          'kids' => string '4' (length=1)
          'profession' => string 'IT' (length=2)
          'submit' => string 'Submit' (length=6)

CodePudding user response:

I believe it's working as intended. If you are not repopulating the fields with the old values then it looks like the page just gets redirected. Also you need to display the validation messages on the page.

https://laravel.com/docs/8.x/validation#repopulating-forms

https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors

CodePudding user response:

Validator doesn't read your status. Make sure you have a default value for this field.

If status is not required, you can change required to nullable, then create a default value after validation.

You need to display an error message to know what the error is.

$errors = $validator->errors();

Usually, this case occurs when you use a select box, check box, or something that can't read a value or empty, null.

The solution is to add a default value or add a hidden field with a default value.

<input type="hidden" name="status" value="0">
<select name="status">
    ...
</select>
  • Related