MeteorJS form submission is refreshing the page with event.preventDefault() enabled - forms

The dating application is built with MeteorJS and the page refreshes whenever I attempt to submit this form to register a user. Also, Meteor.user().services.twitter.profile_image_url doesn't seem to get Twitter image. Please help
imports/ui/components/partials/register-user.html
<template name="registerUser">
<div class="user-card">
<form id="user-form">
<div class="row">
<label for="gender">gender</label>
<select id="gender">
<option value="cis man">cis man</option>
<option value="cis woman">cis woman</option>
<option value="non binary" selected>non binary</option>
<option value="trans man">trans man</option>
<option value="trans woman">trans woman</option>
</select>
</div>
<div class="row">
<label for="prefer">seeking</label>
<select id="prefer">
<option value="cis men">cis men</option>
<option value="cis women">cis women</option>
<option value="everyone" selected>everyone</option>
<option value="non binary">non binary</option>
<option value="trans women">trans women</option>
<option value="trans men">trans men</option>
</select>
</div>
<div class="row">
<button class="button-primary register-user" type="submit" form="user-form">register</button>
</div>
</form>
</div>
</template>
imports/ui/components/partials/register-user.js
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import './register-user.html';
Template.registerUser.onCreated(function () {
this.register_name = Meteor.user().profile.name;
this.register_img = Meteor.user().services.twitter.profile_image_url;
});
Template.registerUser.events({
'submit .register-user' (event) {
event.preventDefault();
var gender = event.target.gender.value;
var prefer = event.target.prefer.value;
if (confirm("by continuing you verify you are a minimum of 21 years old")) {
Meteor.call('register.user', this.register_name, "", gender, prefer, (error) => {
if (error) {
Flash.danger(error.error, 3000);
} else {
Session.set({'complete': true});
}
});
}
},
});
imports/api/methods.js
...
'register.user': function(name, img, gender, prefer) {
RegisteredUsers.insert({ _id: this.userId, complete: true, role: false });
Profiles.insert({
_id: this.userId, name: name, img: img, gender: gender, prefer: prefer, address: "", right: [], matches: [],
});
// redisCollection.hset(this.userId, { "name": name, "img": img, "gender": gender });
},
...

Your example show an error on event mapping:
'submit .register-user' (event) {
You used a class mapping of the button, but the submit event is from the form. Then change it to:
'submit #user-form' (event) {

I think that the issue is because you are mapping the submit event to the button which has the class register-user. Try changing your code to refer to the form from which the submit event comes. You can use its id (user-form) for doing that.
It'd be something like this:
'submit #user-form'(event) {
event.preventDefault();
...
}

Related

How to enable required validation in vuelidate based on onChange event of select field

I have to enable required validation for the input field based on the onChange event of select field. I'm using vuelidate package for form validation in my project. Kindly provide solution to accomplish it.
My Template Code Below:
<template>
<section class="page_blk">
<form #submit="submitForm($event)" class="cryp_form">
<div class="input_ctrl_wrp">
<label for="username">Template</label>
<div class="input_select">
<select #change="getTemplate" v-model="$v.adForm.tnc.$model" name="" id="">
<option value="">Select</option>
<option value="New">New</option>
<option :value="term.idTnCTemplate"
v-for="term in termsList"
:key="term.idTnCTemplate">{{term.title}}</option>
</select>
<i class="fal fa-angle-down"></i>
</div>
</div>
<div class="input_ctrl_wrp">
<label for="username">Title</label>
<div class="input_text">
<input v-model="$v.adForm.title.$model" placeholder="" type="text">
</div>
</div>
<div class="input_ctrl_wrp">
<label for="username">Terms Of Trade</label>
<div class="input_textarea">
<textarea v-model="$v.adForm.content.$model" name="" rows="10"></textarea>
</div>
</div>
</form>
</section>
</template>
My Script Below:
<script>
import { required,requiredIf, decimal, numeric } from "vuelidate/lib/validators";
export default {
data() {
return {
adForm: {
tnc: '',
title: '',
content: '',
}
}
},
validations: {
adForm: {
tnc: {
required
},
title: {
required
},
content: {
required: requiredIf( (abc) => {
console.log('abc',abc)
return true;
})
},
schedule: {
required
}
}
},
methods: {
submitForm(e) {
},
getTemplate(e) {
}
},
mounted() {
}
}
</script>
I want to toggle the validation to required for the title and content field, if the user select new and other option from dropdown. Please provide solution to accomplish it. Thanks in advance.

Ember: recover data from dynamic "select" tags

Using Ember, I am trying to build a form, with <select> droplists populated from stores. I don't know how to recover a value from here to use in a "save" function.
What am I doing wrong?
Here is a simplified version of what I have :
The route accesses to differents models. This works fine :
import Ember from 'ember';
export default Ember.Route.extend({
model() {
var store = this.store;
return Ember.RSVP.hash({
fighters: store.findAll('fighter'),
duels:store.findAll('duel')
});
},
setupController(controller, models) {
var fighters = models.fighters;
var duels = models.duels;
controller.set('fighters', fighters);
controller.set('duels', duels);
}
});
The Controller should save a new duel using the form's values. Here "formdata" is null when the form is submitted
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
save(formdata) {
var newduel = this.store.createRecord('duel', {
fighter1:formdata.fighter1,
fighter2:formdata.fighter2,
});
}
}
});
The template displays droplists OK, but I cannot "connect" to the value in the controller, whatever ##FIGHTERn## I try !
<form {{action "save" formdata on "submit"}}>
<dl>
<dt>
<select value='##FIGHTER1##'>
{{#each fighters as |fighter|}}
<option value={{fighter.id}}>{{fighter.name}}</option>
{{/each}}
</select>
</dt>
<dt>
<select value=##FIGHTER2##>
{{#each fighters as |fighter|}}
<option value={{fighter.id}}>{{fighter.name}}</option>
{{/each}}
</select>
</dt>
</dl>
<button type="submit">Add</button>
<form {{action "save" formdata on "submit"}}>
<dl>
<dt>
<select onchange={{action (mut fighter1) value="target.value"}}>
{{#each fighters as |fighter|}}
<option value={{fighter.id}} selected={{is-equal fighter1 fighter.id}}>{{fighter.name}}</option>
{{/each}}
</select>
</dt>
<dt>
<select onchange={{action (mut fighter2) value="target.value"}}>
{{#each fighters as |fighter|}}
<option value={{fighter.id}} selected={{is-equal fighter2 fighter.id}}>{{fighter.name}}</option>
{{/each}}
</select>
</dt>
</dl>
<button type="submit">Add</button>
Controller:
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
save() {
this.store.createRecord('duel', {
fighter1: scope.get("fighter1"),
fighter2: scope.("fighter2"),
});
}
}
});
This can help you out

Passing id from this.props.data.id to React ReduxForm

My ListItem component was passed data from another component using:
renderData() {
return (
<ListItem
key={data.id}
data={data} />
)
Now in my ListItem component I have access to the data using this.props.data. I want to pass the id, this.props.data.id to reduxForm or my handleFormSubmit function.
I tried adding a hidden input field to pass the id which didn't work?
handleFormSubmit({ id, date }) {
this.props.post( { id, date })
}
And in my render method.....
render() {
const { handleSubmit, fields: { id, date }} = this.props;
return(
<div>
<form className="form-inline" onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<input {...id} type="hidden" name="id" value={this.props.data.id} />
<select className="form-control" {...date} >
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
</form>
</div>
)
}
export default reduxForm({
form: 'day',
fields: ['id', 'date']
})
There must be another way to add the data into my handleFormSubmit method that I'm not seeing?
Figured it out..simply had to send the id in my handleFormSubmit helper function and remove id from the form.
handleFormSubmit({ date }) {
const id = this.props.data.id;
this.props.post( { id, date })
}

meteor template.dynamic load data from session

i have a page split in two:
on left {{> expenseList }}
on right {{> Template.dynamic template="expenseForm" data=expenseData}}
on left its a list
on right its a form to enter elements in the list.
desired behaviour:
1: form is empty and ready to add expenses
2: add an expense, form is cleared and ready for next
3: if i click on the left on the submitted expense from {{> expenseList }} the form is populated with the data of that expense and i can update that entry.
i cannot get it to work though. my form doesn't dynamically change the data. i tried to simplify code and add it below
the page code:
<template name="claimsPage">
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-md-4">
{{> expenseList }}
</div>
{{> Template.dynamic template="expenseForm" data=expenseData}}
</div>
</div>
</template>
the page js
Template.claimsPage.helpers({
expenseData: function() {
var data = Session.get('expenseData');
return data;
}
});
the list js
Template.expenseList.events({
'click a': function(e) {
e.preventDefault();
var data = {
date: this.date,
description: this.description,
_id: this._id
};
Session.set('expenseData', data);
}
});
the form js
Template.expenseForm.helpers({
purposes: function() {
return Purposes.find();
},
expenseData: function(){
return Session.get('expenseData')
}
});
Template.expenseForm.events({
'click #add-expense': function(e){
e.preventDefault();
var template = Template.instance();
var expense = {
date: template.find("[name='date']" ).value,
description: template.find("[name='description']" ).value,
purposeName: template.find("[name='purpose'] option:selected" ).value,
_id: Random.id()
};
Meteor.call('addExpense', expense, function(error, result) {
if (error)
return alert(error.reason);
template.find("form").reset();
});
}
});
the form html
<template name="expenseForm">
<form>
<input name="date" type="text" value="">
<input name="description" type="text" value="">
<select name="purpose">
<option value="">Select a purpose</option>
{{#each purposes}}
<option value="{{this.purposeName}}">{{this.purposeName}}</option>
{{/each}}
</select>
<button id="add-expense" type="button">Submit</button>
</form>
</template>
I would really appreciate your help with this,
Thanks,
Cezar

Auto Refresh Div Content Using jQuery and AJAX passing variables

I am trying to setup a page where the users can select 1 criteria from a select menu and then select the second from another.
I would then like these variables to pass through my auto updating div using ajax so that they are used in the .php that is refreshed.
The select menu is working fine but how would I pass the values through ajax and then make sure it remembered them for the refresh.
FORM
<select id="employee_user">
<option value="">--</option>
<option value="333">Test User</option>
<option value="111">Testing Testing</option>
</select>
<select id="they" onchange="showUser(this.value, employee_user.value)">
<option value="">--</option>
<option value="20120801" class="333" title="Test User">20120801</option>
<option value="20110801" class="333" title="Test User">20110801</option>
<option value="20100801" class="333" title="Test User">20100801</option>
<option value="20120801" class="111" title="Testing Testing">20120801</option>
<option value="20110801" class="111" title="Testing Testing">20110801</option>
</select>
</form>
AUTO REFRESHING DIV
<script>
(function($)
{
$(document).ready(function()
{
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#updatingdiv').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#updatingdiv').show();
},
success: function() {
$('#loading').hide();
$('#updatingdiv').show();
}
});
var $container = $("#updatingdiv");
$container.load("getholidaylog.php");
var refreshId = setInterval(function()
{
$container.load('getholidaylog.php');
}, 9000);
});
})(jQuery);
</script>
<div id="updatingdiv"></div>
<img src="loading.gif" id="loading" alt="loading" style="display:none;" />
and then getholidaylog.php would:
$year = $_GET["year"];
$username = $_GET["username"];
and use for the database query.
EDIT
$j(document).ready(function() {
$j("#year_select").change(function (){
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#updatingdiv').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#updatingdiv').show();
},
success: function() {
$('#loading').hide();
$('#updatingdiv').show();
}
});
var $container = $("#updatingdiv");
var user_select= $j('#user_select').val();
var year_select= $j('#year_select').val();
$container.load('getholidaylog.php',{username:user_select,year:year_select});
var refreshId = setInterval(function()
{
$container.load('getholidaylog.php');
}, 9000);
});
})(jQuery);
**
Following code passing the values to php page successfully
<script type="text/javascript">
$(document).ready(function(){
$("#they").change(function () {
var firstVal = $("#employee_user").val();
var secVal = $("#they").val();
$.ajax({
type: "POST",
data: "year="+ firstVal +"&username="+ secVal,
url: "getholidaylog.php",
success: function(msg){
$("#updatingdiv").html(msg);
}
});
});
});
</script>
but instead of loading the getholidaylog.php into div load the content of respective page into the div
DO Something like this..
Add two hidden fields in your html like this:
-------
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
Now Change your jQuery code like this..
Modify your onchange function of year_select like this and put it separately . Dont include ajax setup in this.
$j("#year_select").change(function (){
$('#userselect').val($('#employee_user').val());
$('#yearselect').val($('#they').val());
});
Now take the value from hidden field.change these lines.
var user_select= $j('#userselect').val();
var year_select= $j('#yearselect').val();
So your Final hmtl markup would look like this:
<select id="employee_user">
<option value="">--</option>
<option value="333">Test User</option>
<option value="111">Testing Testing</option>
</select>
<select id="they" onchange="showUser(this.value, employee_user.value)">
<option value="">--</option>
<option value="20120801" class="333" title="Test User">20120801</option>
<option value="20110801" class="333" title="Test User">20110801</option>
<option value="20100801" class="333" title="Test User">20100801</option>
<option value="20120801" class="111" title="Testing Testing">20120801</option>
<option value="20110801" class="111" title="Testing Testing">20110801</option>
</select>
<select id="employee_user">
<option value="">--</option>
<option value="333">Test User</option>
<option value="111">Testing Testing</option>
</select>
<select id="they" onchange="showUser(this.value, employee_user.value)">
<option value="">--</option>
<option value="20120801" class="333" title="Test User">20120801</option>
<option value="20110801" class="333" title="Test User">20110801</option>
<option value="20100801" class="333" title="Test User">20100801</option>
<option value="20120801" class="111" title="Testing Testing">20120801</option>
<option value="20110801" class="111" title="Testing Testing">20110801</option>
</select>
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
and your code will look like this:
$(function(){
$("#year_select").change(function (){
$('#userselect').val($('#employee_user').val());
$('#yearselect').val($('#they').val());
});
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#updatingdiv').hide();
$('#loading').show();
var user_select= $j('#userselect').val();
var year_select= $j('#yearselect').val();
},
complete: function() {
$('#loading').hide();
$('#updatingdiv').show();
},
success: function() {
$('#loading').hide();
$('#updatingdiv').show();
}
});
$container.load('getholidaylog.php',{username:user_select,year:year_select});
var refreshId = setInterval(function()
{
$container.load('getholidaylog.php');
}, 9000);
})
The low level $.ajax() method contains a variable called data through which you can pass data to the server. So if you want the send data in this case, you can do something like:
$.ajax({
data: {year:1999,username:'Codded'}
});
Same goes for load, the second parameter there is for passing data, so you can do
$("#divId").load('foo.php',{username:'Codded',year:1999});
u can use the data attribute of the ajax function to pass the selected value like this example :
$.ajax({
data: {'name': 'Dan'} // name is the name of the variable and Dan is the value in your case if the selected value
});
Check out your id of select is same as given below because it does not matches with the select box you have coded
var user_select= $j('#user_select').val();
var year_select= $j('#year_select').val();