0

I am learning laravel. I have implemented client side validations in my project using jQuery validations reference.

first my applied client side validations working good. But now I want to access database to give error that already exists or not(server side validation). I have coded that in my controller but because of server side validation my client side validations are not working.

Controller code

public function store(Request $request)
{
    $user = Auth::user();

    $apmcRecord = AgriculturalProduceMarketCommettee::where('apmc_branch_name', $request->apmc_branch_name)
        ->first();
    if ( !empty($apmcRecord) ) {
        return redirect()->back()->with('error', 'APMC Branch already exists!');
    } else {
        $apmc = new AgriculturalProduceMarketCommettee();
        $apmc->apmc_branch_name = $request->apmc_branch_name;
        $apmc->apmc_city_name = $request->apmc_city_name;
        $apmc->is_active = '1';
        $apmc->created_by = 'A'.$user->role_id;
        $apmc->updated_by = '0';
        $apmc->save();
        return redirect()->back()->with('success', 'APMC Branch created');
    }
}

Jquery Validations Code

$("#apmc_create").each(function () {
    $(this).validate({
        rules: {
            apmc_branch_name: {
                required: true,
                maxlength: 30,
                nameRegex: true,
            },
            apmc_city_name: {
                required: true,
            },
        },
        messages: {
            apmc_branch_name: {
                required: "<font color='red' size='1'>Please enter the branch name",
                maxlength: "<font color='red' size='1'>Please enter no more than 30 characters",
                nameRegex: "<font color='red' size='1'>Please enter Letters, numbers, and spaces only",
            },
            apmc_city_name: {
                required: "<font color='red' size='1'>Please enter the city name",

            },
        }
    });
});

Above code not showing me my clide side validations as I included server side error message. I am thinking this is problem due to merging of server and client side validations. One might solution is of using AJAX in jquery validations. But I don't how to use AJAX in MVC framework and in jquery validation so not getting solution of this.

Above validations are for creation. I have to applied same for edit also.

Please help me out how to use ajax in jquery validation above situation or any other solution for this.

Thanks in advance.

Sparky
  • 98,165
  • 25
  • 199
  • 285
ganesh
  • 416
  • 1
  • 11
  • 32
  • `AgriculturalProduceMarketCommettee::where('apmc_branch_name', $request->apmc_branch_name)->first()->count()` would be causing the problem. The `count()` method is unneccessary, and it causes the problem. you only need `AgriculturalProduceMarketCommettee::where('apmc_branch_name', $request->apmc_branch_name)->first()` – iamab.in Nov 21 '19 at 08:30
  • if I removed count then giving me `Integrity constraint violation: ` . Its not checking validations. – ganesh Nov 21 '19 at 09:05
  • which field is causing the `Integrity constraint violation` ? Is it `apmc_branch_name`? – iamab.in Nov 21 '19 at 09:07
  • i am submitting both fields null. It should show me my client side validations that fields are required. Which were showing before including that server side validation. Now I am looking for converting that server side validation to client side using AJAX. If I not done that how will my code work? – ganesh Nov 21 '19 at 09:11
  • Usually there is no chance that the server side validation can cause any effect on client side validation. You should only submit form if the client side validation is successful else prevent form submission. Also you should use [laravel validation](https://laravel.com/docs/5.8/validation) in server side – iamab.in Nov 21 '19 at 09:18
  • Why there is a `each` on `"#apmc_create"`? Where do you call the `validate` method? in form submit event? Can you provide the code? – iamab.in Nov 21 '19 at 09:21
  • First, to test if something is already in the database, use the `remote` rule (this is your ajax). Second, the PHP function called by the `remote` rule must `echo` a `"true"` or `"false"` or JSON encoded string which becomes the custom validation message. [Refer to the docs](https://jqueryvalidation.org/remote-method/). – Sparky Nov 23 '19 at 17:12

0 Answers0