Error Showing Content - please login as admin for details.
Error Showing Content - please login as admin for details.
Formulas affecting Values
We'll show you examples where formulas can be applied to set the Value in many ways.
Important: The feature "Public Use of Edit Form" is disabled
The feature is needed for anonymous users to use the Edit Form. Register your site here https://patrons.2sxc.org/ to get access to the feature.
Set the Value if it's empty
This formula sets the value of a field if it's empty. As long as it's not empty, the user can type anything he/she wants.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasIfEmpty.Title
Field.Value (Formula-Target: Field.Value)
function v1 (data, context) {
if (data.value) return data.value;
return "This field should never be empty";
}
Generate a Field Combining Others
This formula combines the values of 3 fields into a URL Slug.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasCombineValues.UrlKey
Field.Value (Formula-Target: Field.Value)
function v1 (data, context) {
const prefix = (data.Country + '-' + data.Language).toLowerCase();
const titleSlug = data.Title.toLowerCase().replace(/ /g,'-').replace(/[^\w-]+/g,'');
return prefix + "-" + titleSlug;
}
Get Data for a Field from a Web API
Get value for a field from a WebAPI
This formula is very advanced, and will initialize a fields value (if empty) from a WebAPI call.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasWebApiText.Title
Field.Settings.Disabled (Formula-Target: Field.Settings.Disabled)
function v1 (data, context) {
// Only enable once data has been loaded
return !data.Title;
}
Field.Value (Formula-Target: Field.Value)
function v1 (data, context) {
// for demo reasons, don't do this till start is toggled.
if (!data.Start) return data.value;
// If it has a value, keep it, don't look for another
if (data.value) return data.value;
// If data had been stored in the cache by the api-call, return it
if (context.cache.data) return context.cache.data;
// Prevent the API call from running twice
if (context.cache.alreadyRun) return data.value;
context.cache.alreadyRun = true;
// Call the sxc web-api controller from other tutorials
context.sxc.webApi.fetchJson('app/auto/api/basic/hello')
.then(data => {
console.log('got data', data);// log for demo reasons
context.cache.data = data; // set cache for next formula cycle
context.form.runFormulas(); // trigger the next formula cycle
});
// After starting the web-api call, return the current empty value
return data.value;
}
Date Examples
Date - Prefill but leave editable
This formula sets the initial value to the current date (if the field is empty). You can try it by deleting the value, once you leave the field it's updated again.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasDates.RightNow
Field.Value (Formula-Target: Field.Value)
function v1 (data, context) {
// don't change if already has a value
if (data.value) return data.value;
return new Date();
}
Date - Calculate value
This formula sets the third field to the current date/time + an offset the user can choose in a dropdown. Note that since we do some calculations, we must make sure the result is time-zone cleaned.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasDates.PublishWhen
Field.Value (Formula-Target: Field.Value)
function v1 (data, context) {
const now = new Date();
const add = data.PublishIn || 0;
const offset = -now.getTimezoneOffset() / 60;
return now.setHours(now.getHours() + add + offset);
}