Is there a way to change the format of aldeed/meteor-autoform-bs-datepicker to dd/mm/yyyy. I've tried the code below but it doesn't work.
date: {
type: Date,
optional: true,
autoform: {
type: "bootstrap-datepicker",
datePickerOptions:
{
format: "dd/mm/yyyy"
}
}
}
the format
d/mm/yyyy
would return
22/12/2015
Alternatively, the format
D/mm/yyyy
would return
Saturday/12/2015
Check this:
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
Related
I am trying to build a kind of specific simple schema for a collection and i would like to make sure that :
when the user enter a new select in my selectsCollection, he will put one of the options value as selected value.
For example:
SelectsCollection.insert({name:"SelectOne",deviceType:"Select",options:["option1","option2","option3"],value:"option4",description:"This is the first select"});
this do not have to work. I want him to write only one of the 3 options.
Here my schema :
SelectsCollection = new Mongo.Collection('Selects'); //Create a table
SelectsSchema = new SimpleSchema({
name:{
type: String,
label:"Name",
unique:true
},
deviceType:{
type: String,
allowedValues: ['Select'],
label:"Type of Device"
},
options:{
type: [String],
minCount:2,
maxcount:5,
label:"Select Values"
},
value:{
type: String,
//allowedValues:[options] a kind of syntax
// or allowedValues:function(){ // some instructions to retrieve the array of string of the option field ?}
label:"Selected Value"
},
description:{
type: String,
label:"Description"
},
createdAt:{
type: Date,
label:"Created At",
autoValue: function(){
return new Date()
}
}
});
SelectsCollection.attachSchema(SelectsSchema);
Any Idea ? :)
Thanks a lot !
This could be done with custom validation function of a field, inside this function you could retrieve values from other fields:
SelectsSchema = new SimpleSchema({
// ...
options: {
type: [String],
minCount: 2,
maxcount: 5,
label: "Select Values"
},
value: {
label: "Selected Value",
type: String,
optional: true,
custom() {
const options = this.field('options').value
const value = this.value
if (!value) {
return 'required'
}
if (options.indexOf(value) === -1) {
return 'notAllowed'
}
}
},
// ...
});
Look here custom-field-validation for more information
I'm working on a project which I start with using autoValue as
Programs.attachSchema(new SimpleSchema({
createdBy: {
type: String,
autoValue: function() {
return this.userId
},
optional: true,
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: "Created At",
defaultValue: new Date(),
optional: true,
autoform: {
type: 'hidden'
}
}
}));
everything works find until I need to update the information by other users, let's say admin, Programs.update or Programs.insert methods will change the email field.
I tried to use defaultValue for createdBy field but
defaultValue: this.userId
return me null
and i'm not allowed to use
defaultValue: Meteor.userId()
Can anyone explain the difference? I tried use function() {return this.userId} for defaultValue which still got no luck
defaultValue is used by simple-schema for defining default value. There are some quirks so read the docs: https://github.com/aldeed/meteor-simple-schema#defaultvalue
Think of when the code is ran and you will understand why you can't use Meteor.userId() or this.userId for defaultValue. The schema is ran once at startup.
What allows autoValue to work is that it returns a function. The function is ran during db updates/inserts. Read over the docs to fully understand it: https://github.com/aldeed/meteor-simple-schema#autovalue
Now, if I understand your question properly, you have issues with autoValue when an admin comes along and modifies the document? Causing the createdBy to be set to the admin's id? To solve something like that, you just need to be more specific with your autoValue function.
See if this code helps guide you in the proper direction:
Programs.attachSchema(new SimpleSchema({
createdBy: {
type: String,
autoValue: function() {
if (this.isInsert) {
return this.userId;
} else if (this.isUpsert) {
return { $setOnInsert: this.userId };
}
this.unset(); // Prevent user from supplying their own value
return undefined;
},
optional: true,
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: 'Created At',
defaultValue: new Date(),
optional: true,
autoform: {
type: 'hidden'
},
autoValue: function() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return { $setOnInsert: new Date() };
}
this.unset(); // Prevent user from supplying their own value
return undefined;
},
}
}));
You should try this snippet,
new SimpleSchema({
// ...
createdBy: {
autoValue() {
return Meteor.userdId();
}
}
// ...
})
Now the explanation, Your problem is more likely related with the this binding, this.userId, was called from SimpleSchema context in this way this does not have any userId() method, you should use the full namespace in this case Meteor.userId();
A very cool explanation on this binding I recommend you to read
This binding
I am trying to use autoValue in my schema
Posts.schema = new SimpleSchema({
title: { type: String },
description: { type: String },
posted: { type: Date,
autoValue: function (){
return new Date;
},
},
likes: { type: Number, defaultValue: 0, optional: true },
dislikes: { type: Number, defaultValue: 0, optional: true, },
author: { type: AuthorSchema },
votes: { type: [AuthorSchema], optional: true }
});
Posts.attachSchema(Posts.schema);
I am using this schema for validations here:
export const addPost = new ValidatedMethod({
name: 'Posts.addPost',
validate: Posts.schema.validator(),
run(post) {
if (!this.userId)
throw new Meteor.Error('403', 'You must be logged-in to reply');
Posts.simpleSchema().clean(post);
Posts.insert({
title: post.title,
description: post.description,
author: {
userId: this.userId,
vote: 0
}
});
}
});
It does not work. I get an error message
Posted is required [validation-error]
Am i doing something wrong? Do i need to make Posted field optional?
I tried to change the insert method by providing default value for posted: new Date(). Did not work either. Please help.
Fixed it by calling validator with { clean : true, filter : false }
Here is code used in simpleschema definition.I tried to use the similar one in my project but as you can see it is not working.
{
items:{
type: Array,
optional: true,
minCount: 0,
maxCount: 5
},
"items.$": {
type: Object,
optional: true
},
"items.$.name": {
type: Date,
optional: true,
autoform: {
afFieldInput: {
type: "bootstrap-datetimepicker"
}
}
}
}
I was facing the issue because of some bug in autoform-datetimepicker Api.
I added the following line of code under autoform-bs-datepicker.js
`
this.autorun(function () {
var data = Template.currentData();
**if(!data.value)
data.value=$('#'+data.atts.id).val();**`
var todate = new Ext.form.DateField({
format: 'd/m/Y',
fieldLabel: '',
id: 'txtExpireDate',
name: 'txtExpireDate',
width: 150,
allowBlank: false,
value: '',
renderTo: 'divDateExpire'
});
This code to display only date. How to use datetime format?
Thank you
In Ext.Form.DateField we're not able to show time directly when selecting a date in date picker. For that we add listeners:
var dtpIssueDate = new Ext.form.DateField({
listeners: {
select: function(dtpIssueDate, date) {
BindTimeWithDate(date);
}
}
});
//Bind time with date selected in the picker
function BindTimeWithDate(date) {
try {
var currentTime = new Date()
// Get a current hours and add with date.
date.setHours(currentTime.getHours());
// Get a current minutes and add with date.
date.setMinutes(currentTime.getMinutes());
dtDateTimeValue = date.format('dd/MM/yyyy HH:mm');
dtpIssueDate.SetText(dtDateTimeValue);
}
catch (e) {
alert(e.message);
}
}
There is no component in extjs library which can display date & time both. And thus, you cannot use date time format in one. The closest one can get to is with this.
Hope this helps.