I am new to front-end validations, so I was hoping for a little help over here.I am using JQuery Validate Plugin to validate my form before submission. Here is a short sample of my form :
<form method="POST" action="{{ route('employees.store') }}" name="createEmployeeForm" enctype="multipart/form-data">
<input name="_token" type="hidden" value="{{ csrf_token() }}" />
<input type="text" id="FirstName" name="FirstName">
<input type="text" id="LastName" name="LastName">
<input type="file" id="employee-image" name="employee_image">
<button id="submit_btn" type="submit">Submit</button>
</form>
I added set of rules to my inputs "FirstName and LastName". When I press on the "Submit" button it validates the form first before submission which is Good, however after I add rules and custom validation method to (input of type "file"), the form is submitted to server-side and no previous validations occurs. Here is my js:
$(document).ready(function() {
$.validator.addMethod('filesize', function(value, element, param) {
return this.optional(element) || (element.files[0].size <= param)
});
$("form[name='createEmployeeForm']").validate({
rules: {
FirstName: "required",
LastName: "required",
employee_image: {
accept: "jpg,png,jpeg,gif",
filesize: 1048576
}
},
// Specify validation error messages
messages: {
employee_image: {
accept: "uploaded file should have an image-extension : jpg,png,jpeg,gif and less than 1 MB "
},
submitHandler: function(form) {
form.submit();
}
});
});
I tried preventdefault on the submit button and return false in the submit handler, but nothing seems to work out and I don't want to submit my form via ajax. Guidance is appreciated
acceptmethod is not part of the core, you also need to include the additional-methods file to get it. Also,messagesshould not be insiderules, it should be on its own. See jsfiddle.net/Arkni/yasutt29acceptexits in the additional-methods file. As for themessagesbeing insiderulesit was a typo when I wrote the snippet.I will update it. Thanks again :) @Arkni