I am trying to add a hidden field for the user registration. The problem is not with the field itself but with its value. I want to parse it a default value. This is my code:
Accounts.ui.config({
requestPermissions: {},
extraSignupFields: [{
fieldName: 'name',
fieldLabel: 'Name',
inputType: 'text',
visible: true,
validate: function(value, errorFunction) {
if (!value) {
errorFunction("Please write your first name");
return false;
} else {
return true;
}
}
},{
fieldName: 'status',
fieldLabel: 'Status',
inputType: 'text',
value: 'somevalue',
visible: false,
}]
});
I want to add the value to the field 'status'.
Actually, I found the answer. The option is the following code in the
server folder:
Accounts.onCreateUser(function(options, user) {
if (options.profile) {
user.profile = options.profile;
}
user.profile.status = "sth";
return user;
});
Looking at the implementation of signup in that package, there is no way to set a default value. The code just creates an object in signup() and grabs whatever existing values from the form that it can.
Related
One of my fields should be either mandatory(required) or not depending on one boolean variable. No matter if it changes or not the field stays required. Not sure what's wrong with my expressionProperties templateOptions.required as this is what triggers that change.
This is part of my formly form
vm.showDeleteButton = false;
vm.fields = [
{
className: 'row',
fieldGroup: [
{
className: 'col-xs-6',
key: 'transferDate',
type: 'datepicker',
templateOptions: {
label: 'Deallocation Date',
type: 'text',
datepickerPopup: 'dd/MM/yyyy',
minDate: vm.model.minDate,
maxDate: vm.model.maxdate,
},
expressionProperties: {
'templateOptions.required': !vm.showDeleteButton
}
}
]
}
];
Also tried this
expressionProperties: {
'templateOptions.required': function() {
if(!vm.showDeleteButton) {
return true;
else {
return false;
}
}
}
I've read the formly expressions documentation but that doesn't help either.
HTML as requested
<formly-form model="vm.model" fields="vm.fields" options="vm.options" form="vm.form"></formly-form>
Kind of 'hacked' it to work.
Changed vm.showDeleteButton to vm.model.showDeleteButton and in my expressionProperties wrote this
expressionProperties: {
'templateOptions.required': '!model.showDeleteButton'
}
I have a fiddle which demonstrates this strange behaviour. In a nutshell, I have a custom field which extends Ext.DataView. I need to extend dataview, because this field is supposed to have dynamic contents. This is how my field is defined:
Ext.define('Ext.ux.SimpleList', {
extend: 'Ext.DataView',
alias: 'widget.simplelist',
requires: [
'Ext.XTemplate'
],
itemSelector: 'div.record',
isFormField: true,
getFieldIdentifier: function () {
return this.name;
},
getModelData: function() {
var me = this;
var data = {};
data[me.getFieldIdentifier()] = me.getValue();
return data;
},
getSubmitData: function() { return {}; },
submitValue: true,
isValid: function () { return true; },
isDirty: function () { return true; },
validate: function () { return true; },
isFileUpload: function() { return false; },
constructor: function(config) {
Ext.applyIf(config, {
tpl: [
'{% var name = this.owner.name; %}',
'<tpl for=".">',
'<div class="record"><input record-id="{id}" type="checkbox" name="{[name]}" /> {label}</div>',
'</tpl>',
{compiled: true}
]
});
this.callParent([config]);
},
getValue: function () {
var cb = this.el.select("input").elements, i, res = [];
for (i in cb) {
if (cb.hasOwnProperty(i) && cb[i].checked) {
res.push(cb[i].getAttribute("record-id"));
}
}
return res;
},
setValue: function (values) {
//not yet implemented
}
});
And this is how I add this field to a form:
Ext.create("Ext.form.Panel",{
renderTo: Ext.getBody(),
items:[{
xtype: "textfield",
name: "text"
},{
xtype: "simplelist",
name: "list",
store: {
fields: ["id", "label", "checked"],
data: [{"id": "1", "label": "One"},{"id": "2", "label": "Two"}]
}
},{
xtype: "button",
text: "Submit",
handler: function () {
var frm = this.up("form").getForm();
console.log(frm.getFieldValues()); // it' ok
//simplelist field is not submitted
this.up("form").getForm().submit({
url: "/"
});
}
}]
});
As you can see, when I submit the form I log to the console form field values. And what is interesting about that, is that I see my custom field among those field values. So, I have a field with isFormField set to true, this field is in the list returned by form getFields() method and this field is also among those values returned by form getFieldValues() method, but still this field is not submitted. What is wrong with that and how can I fix it?
Your code uses basicForm.getFieldValues(), which calls basicForm.getValues() with some parameters, while the form while submitting uses the same method with different parameters. One of those parameters is useDataValues, which decides whether to use the getModelData or getSubmitData.
You are returning empty object in your getSubmitData method, which prevents it to correctly get the values.
All you need to change, for both methods to work in your current state, is this:
getSubmitData: function() {
return this.getModelData();
}
I use Formly for creating my forms in angularJS
This is my field
$scope.postFields = [
{
key: 'title',
type: 'input',
templateOptions: {
label: "Title",
// required: true,
minlength: 2,
},
validation: {
messages: {
required: function(viewValue, modelValue, scope) {
return scope.to.label + ' is required'
}
}
}
}
]
and I'm trying to access my fields as follows
function failure(response) {
console.log("failure", response)
_.each(response.data, function(errors, key) {
_.each(errors, function(e) {
$scope.form[key].$dirty = true;
$scope.form[key].$setValidity(e, false);
});
});
}
my formly form
<formly-form form="postForm" model="model" fields="postFields" class="col-md-4">
<button type="submit" class="btn btn-primary" ng-click="addPost()">Submit</button>
</formly-form>
but of course I'm getting this error:
TypeError: Cannot read property 'title' of undefined
it's on this line
$scope.form[key].$dirty = true;
do anyone of you know how to reference created formly fields the right way?
If you want to be able to reference the fields from the form, you could provide a name attribute to your fields. The name is generated by default. It's one of the nice things that angular-formly provides (you don't have to think about it). But if you want to reference it directly with a specific key (as you are) then you'd be best served by providing one yourself.
So you'd do something like this:
$scope.postFields = [
{
key: 'title',
name: 'title',
type: 'input',
templateOptions: {
label: "Title",
// required: true,
minlength: 2,
},
validation: {
messages: {
required: function(viewValue, modelValue, scope) {
return scope.to.label + ' is required'
}
}
}
}
]
Alternatively, you could create a fieldTransform to do this automatically (just assign the name the same as the key). Or in your error handler, you could look up the NgModelController from the field's formControl property like this:
function handleError(fields, response) {
_.each(fields, function(field) {
if (response.data[field.key]) {
field.formControl.$setDirty()
_.each(response.data[field.key], function(e) {
field.formControl.$setValidity(e, false)
})
}
})
}
That's probably the best solution :-) Good luck!
I'm using selectize.js like email contacts. But when I get mail list from database selectize shows undefined. When I type return value manuelly everything is fine. How can I populate it? Here is my code samples.
Return Value:
"[{"name":"Test User 1","id":"1"},{"name":"Test User 2","id":"2"}]"
C#
public JsonResult GetMails(){
var list = db.Members
.Where(x => x.IsActive == Status.Active)
.OrderBy(x => x.FullName)
.Select(c => new
{
name = c.FullName,
id = c.Id.ToString()
});
return Json(list, JsonRequestBehavior.AllowGet);
}
Javascript
<script type="text/javascript">
$(function () {
$.get("/mail/getmails", function (data) {
$("#MailReceivers").selectize({
persist: false,
plugins: ['remove_button'],
maxItems: null,
valueField: 'id',
labelField: 'name',
searchField: ['name', 'id'],
options: JSON.stringify(data),
create: function (input) {
return {name: input};
}
});
});
});
</script>
The answer is first parse json and than stringfy.
options: JSON.parse(JSON.stringify(data))
Hi I'm very new to 'Ext JS' (about 4 days) and have to edit 'Ext JS' form. That form is to edit existing entry. So I have to update 'project name' field in this form and need to follow following conditions.
Users can submit form with its old project name (old old project
name means project name when form is load).
When user select existing project name from suggestion drop down (except old project name), need to show error.
Basic idea of this conditions is prevent duplicate project names.
So I try to do this with 'validator' but I can't find a way to compare old project name and new project name.
Is there any method to compare those values ??
Any other way to validate this field ???
this is what I have done so far...
_est.project.view.panel.updateprojectname.combox = function (projectdetails) {
var nstore = _est.project.view.panel.updateprojectnstore();
var combo = Ext.create('Ext.form.ComboBox', {
fieldLabel: _est.project.text.nameText,
store: nstore,
labelSeparator: ' ',
vtype: 'text',
afterLabelTextTpl: _est.afterLabelTextRequiredTpl,
msgTarget: 'side',
displayField: 'projectname',
valueField: 'projectname',
labelWidth: 130,
maxLength: 200
name: 'projectname',
allowBlank: false,
pageSize: true,
triggerAction: 'query',
autoSelect: true,
minChars: 0,
itemId : 'project-name-field',
value: Ext.htmlDecode(projectdetails.projectname),
hideTrigger: true,
anchor: '95%',
validator: function(oldValue) {
var existnstore = Ext.StoreMgr.lookup("ptoject-project-names-get-store");
if(existnstore){
var nstore = existnstore.findExact('projectname',oldValue);
if(nstore < 0){
return true;
}else{
return 'Job name already exist.';
}
}
},
listConfig: {
getInnerTpl: function () {
return '<a class="search-item">' +
'<span>{projectstate}</span>' +
'{projectname}' +
'</a>';
}
},
});
return combo;
}
any suggestions or opinions to do this :) thanks in advance..
I change 'validator' function and this work for me
validator: function(Value) {
var existname = projectdetails.projectname;
if(Value !== existname){
var existnamestore = Ext.StoreMgr.lookup("update-ptoject-project-names-get");
if(existnamestore){
var namestore = existnamestore.findExact('projectname',Value);
if(namestore < 0){
return true;
}else{
return 'Job name already exist. Please select different job name.';
}
}
}
return true;
}