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.