Validation only applies to first email input - jquery-validate

I'm using the jQuery Validate plugin to make sure valid email addresses are entered on this page.
My problem is that this code only validates the first input that has type="email".
Is there a workaround for this so that all of them are validated? It certainly doesn't seem to be the case in the plugin's demo.
<script>
$(document).ready(function()
{
$('.form-inline').validate(
{
rules:
{
email:
{
required: true,
email: true
}
},
errorLabelContainer: ".hidden",
errorPlacement: function(error, element)
{
error.insertAfter(element);
}
});
});
</script>
<!-- Get notified modal 1 -->
<div class="modal fade" id="getNotifiedModal1" tabindex="-1" role="dialog" aria-labelledby="getNotifiedLabel1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="getNotifiedModal1Label">Sorry, this feature is unavailable in the demo version.</h4>
</div>
<div class="modal-body">
<p id="p">But why not get notified when the full version of Shopaholic launches?</p>
<form role="form" class="form-inline" method="POST">
<input type="email" name="feature_unavailable_email" class="inputEmail form-control" placeholder="Enter email">
<button type="submit" class="btn btn-primary">Notify me</button>
</form>
<div type="hidden" class="hidden" class="p"></div>
</div>
</div>
</div>
</div>
<!-- Get notified modal 2 -->
<div class="modal fade" id="getNotifiedModal2" tabindex="-1" role="dialog" aria-labelledby="getNotifiedLabel2" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="getNotifiedModal2">Get notified when Shopaholic launches!</h4>
</div>
<div class="modal-body">
<form role="form" class="form-inline" method="POST">
<input type="email" name="banner_email" class="inputEmail form-control" placeholder="Enter email">
<button type="submit" class="btn btn-primary">Notify me</button>
</form>
<div type="hidden" class="hidden" class="p"></div>
</div>
</div>
</div>
</div>
<!-- About modal -->
<div class="modal fade" id="aboutModal" tabindex="-1" role="dialog" aria-labelledby="aboutLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="aboutModal">About Shopaholic</h4>
</div>
<div class="modal-body">
<p>Shopaholic turns your new tab page into a feed of sale items from your favourite clothes stores.</p>
<p>Simply pick which stores you want to add to your feed, filtering by country and gender, and you're good to go!</p>
<p id="p">Enter your email address below to get notified when Shopaholic launches.</p>
<form role="form" class="form-inline" method="POST">
<input type="email" name="about_email" class="inputEmail form-control" placeholder="Enter email">
<button type="button" class="btn btn-primary">Notify me</button>
</form>
<div type="hidden" class="hidden" class="p"></div>
</div>
</div>
</div>
</div>

The fact that you're using an id as your selector might be causing the issue. Instead of $('#form') try to use $('.form-inline') (the class all your forms seem to be sharing)
EDIT:
Ok so I looked pretty deep into it and here is what you should so.
$(document).ready(function()
{
$('.form-inline').each(function () {
$(this).validate(
{
errorPlacement: function(error, element)
{
element.parent("form").next("div.p").html(error);
}
});
$(this).children("input[type=email]").rules("add", {
required: true,
email: true
});
});
});
I think you should be careful with your naming convention though. Like try to call your error containers something more meaningful like "error-console"
See how instead of calling .validate() on one specific element, you have to iterate through all of them using a .each() loop.
Also, jQuery Validator use the name attribute as a convention when declaring rule...so if you want to use a class you need to call that .rules("add", {}) in order to make it work
I hope this helped :)
ADDITIONAL EDIT:
Noticed your last form doesn't get validated .. it's because you have your last button set as type="button" instead of type="submit"

You cannot have duplicate id since id must be unique, so the solution is to replace the duplicated id by using class and it should work.

Related

How do I solve the modal update issues in Laravel?

I have a modal in which I want to update my Question but the modal keeps updating the same question so can someone help me because my modal keeps taking the same $id even if I click on different entries.
This is my QuestionController
public function update($questionId)
{
$this->validate(request(),[
'label' => 'required',
]);
$data = request()->all();
$question = Question::find($questionId);
$question->label = $data['label'];
$question->save();
return redirect('/question');
}
this is my Modal in the blade.php file
<!-- Modal Edit -->
<div class="modal" id="editMcqModal" tabindex="-1" role="dialog"
aria-labelledby="editMcqModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenteredLabel">Edit Question</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="" action="/question/{{$question->id}}" method="post">
#csrf
#method('PATCH')
<div class="form-group">
<textarea name="label" class="form-control" placeholder="Please Enter Question" cols="5"
row="15">{{$question->label}}</textarea>
</div>
<div class="form-group">
<button type="submit" name="add" class="btn btn-success float-right">Update
Question</button>
</div>
</form>
</div>
</div>
</div>

how to show different bootstrap modals on a single page

I am trying to open multiple modals on a single page but their ids are conflicting with each other. I got this code from bootstrap-4 documentation,
Modal 1 is works fine but modal 2 fails to work, I wants that both of them to work separately
code:
modal1
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal">
Add New
</button>
<!-- Modal Add Owner -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Owner</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" action="{{ route('owner.store') }}">
#csrf
<div class="form-group">
<label for="">Add Owner Name</label>
<input type="text" name="owner_name" class="form-control" id="" placeholder="Owner Name">
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
modal2:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Show
</button>
<!-- Modal Show Owner-->
<div class="modal fade" id="exampleModalshow" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabe2" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">View Owner</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" action="{{ route('owner.update', $owner->id) }}">
#csrf
#method('PATCH')
<div class="form-group">
<label for="">Owner Name</label>
<input type="text" name="owner_name" class="form-control" value="{{ $owner->owner_name }}">
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
Change data-target on the second modal button to match the modal id
<button data-target="#exampleModalshow">
... rest of the code
</button>

how to work with laravel modal using ajax

Iam working on a simple laravel project where i have a page with http://loocalhost:8000/home as url .i have a button on the page.clicking the button will open a modal where i will have a form.What my problem is whenever i submit the form in the modal the data should be saved in database .i want to implement it by using ajax and then pass it to controller.but i don't know how to implement it exactly.
my modal in
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">New post</h4>
</div>
<div class="modal-body">
<form id="frmPost" name="frmPost" class="form-horizontal" novalidate="">
<div class="form-group error">
<label for="inputName" class="col-sm-3 control-label">Title</label>
<div class="col-sm-9">
<input type="text" class="form-control has-error" id="title" name="title" placeholder="Title" value="">
</div>
</div>
<div class="form-group">
<label for="inputDetail" class="col-sm-3 control-label">Description</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="description" name="description" placeholder="Description" value="">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="btn-save" value="">Add</button>
<!-- <input type="hidden" id="product_id" name="product_id" value="0"> -->
</div>
</div>
</div>
my home page with url :http://localhost:8000/home
<button id="btn_add" name="btn_add" class="btn btn-default">New Post</button>
my ajax code :
$('#btn_add').click(function(){
var url = "http://localhost:8080/home";
$('#btn-save').val("add");
$('#frmPost').trigger("reset");
$('#myModal').modal('show');
$('#btn-save').click(function(e){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf_token()"]').attr('content')
}
})
var formData = {
'title':$('#title').val(),
'description' :$('#description').val()
}
$.ajax({ //line 28
url: url,
type: 'POST',
data: { userData: formData },
success: function()
{
alert("Settings has been updated successfully.");
}
});
});
});
routes.php code:
Route::get('/home', 'HomeController#index');//route for home page after login
Route::post('/home', 'HomeController#addPost');//route to go after submitting modal data.
But i am getting error i dont know what the issue.can someone help me?
My error:
jquery.js?27d9:9536 POST http://localhost:8080/home net::ERR_CONNECTION_REFUSED

how to show popup using ajax output

i want to show a popup message about to login the user. here iam using ajax function.
if the success: part is true i want to show the popup message. pls help me thanks in adv.
<button data-toggle="modal" data-target="#squarespaceModal" class="btn green btn-success" onclick="going('<?echo $user_id;?>','<?echo $event_id;?>')">Going</button>
<div class="modal fade" id="squarespaceModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h3 class="modal-title" id="lineModalLabel">My Modal</h3>
</div>
<div class="modal-body">
<!-- content goes here -->
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div class="modal-footer">
<div class="btn-group btn-group-justified" role="group" aria-label="group button">
<div class="btn-group" role="group">
<button type="button" class="btn btn-default" data-dismiss="modal" role="button">Close</button>
</div>
<div class="btn-group btn-delete hidden" role="group">
<button type="button" id="delImage" class="btn btn-default btn-hover-red" data-dismiss="modal" role="button">Delete</button>
</div>
<div class="btn-group" role="group">
<button type="button" id="saveImage" class="btn btn-default btn-hover-green" data-action="save" role="button">Save</button>
</div>
</div>
</div>
</div>
</div>
</div>
function going()
function going(user_id,event_id)
{
$.ajax({
url: "<?echo base_url()?>events/event_going",
type: 'post', // HTTP METHOD
data:
{user_id:user_id,event_id:event_id },
success: function(data)
{
if(data='true')
{
//pls check & add any suggsns
$('#squarespaceModal').dialog(data);
}
// document.getElementById('mysubmit').value="active";
}
});
}
controller
public function event_going()
{
$user_id=$this->input->post('user_id');
$event_id=$this->input->post('event_id');
$temp=$this->session->userdata('user');
$going=$this->EM->is_going($event_id,$user_id);
$going1=$this->EM->is_going1($event_id,$user_id);
if($temp=="")
{
echo "Please log in";
}
else if($going==$user_id)
{
echo "Already confirmed as going..";
}
else if($going1==$user_id)
{
echo "Are you sure you want to change... Press ok to continue";
$this->EM->event_updation($event_id,$user_id);
}
else
{
$data=array('event_id'=>$event_id,'ev_going'=>$user_id);
$this->EM->eventgoing($data);
}
}
What you can do is, instead of showing a new popup, you can get the content of popup from ajax. What i mean is, on click of a button, call ajax function, get the content and have a div inside the modal-body, for example
<div class="dynamic-data">
</div>
and in ajax success function do like this
success: function(data){
$(".dynamic-data").html(data);
}

form post not working in laravel

I have a form as follows
<form class="form-inline" id="inviteForm" action="http://localhost:8000/team/sendInvitation" method="post">
<div class="modal fade" id="sendInvite" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Send an invite</h4>
</div>
<div class="modal-body">
<form class="form-vertical">
<!-- Inviteename Form Input -->
<div class="form-group">
<label for="inviteename">Name :</label> <input class="form-control" required="required" name="inviteename" type="text" id="inviteename"> </div>
<!-- Email Form Input -->
<div class="form-group">
<label for="email">Email :</label> <input class="form-control" required="required" name="email" type="email" id="email"> </div>
<!-- Invitemessage Form Input -->
<div class="form-group">
<label for="invitemessage">Message :</label> <textarea class="form-control" required="required" name="invitemessage" cols="50" rows="10" id="invitemessage"></textarea> </div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="uk-button" data-dismiss="modal">Close</button>
<i class="uk-icon-send"></i> Send Email
</div>
</div>
</div>
</form>
and my route is as follows
Route::post('sendInvitation',['uses'=>'TeamsController#sendInvitation','as'=>'invitation.send']);
and the UI code is as follows
<form class="form-inline" id="inviteForm" action="{{URL::route('invitation.send')}}" method="post">
#include('admin.teams.partials.sendinvite')
</form>
and the controller code is as follows :
public function sendInvitation()
{
dd(Input::all());
}
I am expecting to see the input values dumped on the screen , but right now nothing is happening.
The include file has the UI for modal dialog.
My question is why is the form not being posted. I am pretty sure I am missing something which is very silly, but if you can point it out for me, it would be praiseworthy.
Thanks
The problem is the nested form structure.
http://www.w3.org/TR/xhtml1/#prohibitions
Remove <form class="form-vertical"> and replace with a normal <div> or <span>.

Resources