0

So when I applied laravel inbuilt validations it was working fine. But when I'm applying jquery validations is not working and give me an error " SQLSTATE[HY000]: General error: 1364 Field 'profile_img' doesn't have a default value"

**home.blade.php**

This is my main home page which has form for creating students details.

```
<div class="jumbotron">
    <form name="form" id="frm" action="{{ url('/addStud') }}" method="POST" class="col-md-4 d-flex flex-column justify-content-center">
        @csrf
        <div class="form-group ">
            <label for="name">NAME</label>
            <input type="text" class="form-control" name="name">
            <span class="text-danger">@error('name'){{$message}}@enderror</span>
        </div>
        <div class="form-group">
            <label for="rollno">ROLL NO</label>
            <input type="text" class="form-control" name="rollno">
            <span class="text-danger">@error('rollno'){{$message}}@enderror</span>
        </div>
        <div class="form-group">
            <label for="address">Email </label>
            <input type="email" class="form-control" name="email">
            <span class="text-danger">@error('email'){{$message}}@enderror</span>
        </div>
        <div class="form-group">
            <label for="phone">PHONE NO</label>
            <input type="tel" class="form-control" name="phoneno">
            <span class="text-danger">@error('phoneno'){{$message}}@enderror</span>
        </div>
        <div class="form-group">
            <label for="dob">DOB</label>
            <input type="date" class="form-control" name="dob">
            <span class="text-danger">@error('dob'){{$message}}@enderror</span>
        </div>
        <div class="form-group">
            <label for="dob">COURSE</label>
            <input type="text" class="form-control" name="course">
            <span class="text-danger">@error('course'){{$message}}@enderror</span>
        </div>
        <div class="form-group mb-2">
            <input type="file" class="form-control" name="profile_img">
            <span class="text-danger">@error('profile_img'){{$message}}@enderror</span>
        </div>
        <div>
            <button type="submit" class="btn btn-primary text-uppercase">create</button>
            <a href="/list" class="btn btn-secondary text-uppercase">view list</a>
        </div>

    </form>

</div>

<!-- jquery validation before form submit -->
<script>
    // $.validator.addMethod('filesize', function(value, element, param) {
    //     return this.optional(element) || (element.files[0].size <= param)
    // });

    $("form[name = 'form']").validate({

        rules: {
            name: {
                required: true,
            },
            rollno: {
                required: true,
            },
            email: {
                required: true,
                email: true,
            },
            phoneno: {
                required: true,
                digits: 10,
            },
            dob: {
                required: true,
                date: true
            },
            course: {
                required: true,
            },
            profile_img: {
                required: true,
                extension: "png|jpg|PNG|JPEG",
                filesize: 1048576,
            },
        },
        messages: {
            name: {
                required: 'name is required',
            },
            rollno: {
                required: 'roll no is required',
            },
            email: {
                required: 'email is required',
            },
            phoneno: {
                required: 'phone no is required',
            },
            dob: {
                required: 'dob is required',
            },
            course: {
                required: 'course is required',
            },
            profile_img: {
                required: 'image is required',
            },
        }

    })
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/additional-methods.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- <script src="../js/validations.js"></script> -->

@endsection

This is my controller in which I have commented the inbuilt laravel validations which is working fine.

**StudentController.php**
```
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;


class StudentController extends Controller
{
    // public function index()
    // {
    //     return view('');
    // }

    public function create(Request $req)
    {
        $student = new Student;
        $student->name = $req->name;
        $student->rollno = $req->rollno;
        $student->email = $req->email;
        $student->phoneno = $req->phoneno;
        $student->dob = $req->dob;
        $student->course = $req->course;
        // $student->profile_img = $req->profile_img;

        if ($req->hasFile('profile_img')) {
            $file = $req->file('profile_img');
            $extension = $file->getClientOriginalExtension();
            $filename = time() . '.' . $extension;
            $file->move('uploads/students/', $filename);
            $student->profile_img = $filename;
        }

        

        // $req->validate([
        //     'name' => 'required|regex:/^[\p{L}\s-]+$/|max:255',
        //     'rollno' => 'required',
        //     'email' => 'required|email',
        //     'phoneno' => 'required|digits:10',
        //     'dob' => 'required',
        //     'course' => 'required|string',
        //     // 'profile_img' => 'required|mimetypes:image/jpeg,image/png,image/bmp,image/svg',


        // ]);

        $student->save();
        return redire`enter code here`ct('list');
    }


```
2
  • jquery validation not working show your console error then i will help you Commented Jul 2, 2021 at 12:47
  • on console it is only showing this, POST 127.0.0.1:8000/addStud 500 (Internal Server Error) Commented Jul 2, 2021 at 12:51

3 Answers 3

1

With HTML you must specify that form is sending files to server using

enctype="multipart/form-data"

so add that to your tag

The probblem you are encountering is because your file is uploaded into the input, so jQuery validates it as present, but when you send the data to the server, your request does not contain that input, so it crashes

Sign up to request clarification or add additional context in comments.

3 Comments

still, it is giving me the same error after adding enctype.
Try adding dd($req); into controller to see if what input is there in place of the file
exactly where?? Actually, I'm new to Laravel so!
0
array:8 [▼
  "_token" => "WyygV0eze9aCO2WQlZMVQyJsSxe6JwFUtSptF6IS"
  "name" => "Tia"
  "rollno" => "1234567"
  "email" => "[email protected]"
  "phoneno" => "9854678955"
  "dob" => "2021-07-16"
  "course" => "BA English"
  "profile_img" => Illuminate\Http\UploadedFile {#295 ▼
    -test: false
    -originalName: "ten.png"
    -mimeType: "image/png"
    -error: 0
    #hashName: null
    path: "C:\xampp\tmp"
    filename: "php8E0D.tmp"
    basename: "php8E0D.tmp"
    pathname: "C:\xampp\tmp\php8E0D.tmp"
    extension: "tmp"
    realPath: false
    writable: false
    readable: false
    executable: false
    file: false
    dir: false
    link: false
  }

so after entering details I'm getting this when I returned dd($req->all());

but if I'm not entering details itis showing another column as cannot be null

3 Comments

you have to show you console error for jquery validation
at the beginning of the function, dd() will print any variable that you put into it
there no console error for jquery validatation
0

profile_img this column name in table update your column by_default null .

Then you will not get this error .

I hope you got your solution .

4 Comments

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into students (name, rollno, email, phoneno, dob, course, updated_at, created_at) values (?, ?, ?, ?, ?, ?, 2021-07-02 12:15:18, 2021-07-02 12:15:18)) now it is showing another column. !!
update name colom by default null also dd($req->all()); and share your dd then i will guide you .
dd($req->all()); //here dd then you will get your result $student = new Student;
profile_img: { required: 'image is required', }, update with profile_img: { required: 'image is required', } also remove from coln profile_img: { required: true, extension: "png|jpg|PNG|JPEG", filesize: 1048576, } i think there is conflict some thing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.