248 Posts
Giuse
7 years ago
Topic

Hi, I am trying to use computational rules to concatenate values of some fields AND some custom text, like simple "-" so separate field values.

I have had no luck, read many posts, tried the "free" formula, but it seems the free formula deals only with numbers.... I know there is SD Concat plugin, but no way of doing that with native Seblod? SD Concat btw has the limitation of just "Value" variation supported to be shown, so for example no way of setting the defaul value that user may change.

Thanks

Giuse

Get a Book for SEBLOD
4229 Posts
Kadministrator
7 years ago
14
Level 1

You can put custom text in a hidden field with storage None. Then you can use it in concatenation.

248 Posts
Giuse
7 years ago
13
Level 2

Thanks Klas but...the order of fields is not respected in the concatenation, they are put in the same order in which the fields appear in the content type and not in the order of the computational field, this is a problem...

By the way, SD Concat is no more updated, any other way to have a default value of a field as concatenation of other fields, a default value that can be changed by the user? A Javascript in the "other stuff" of the field...? It seems to me quite a common need.

thanks a lot

Giuse

1283 Posts
Bucklash
7 years ago
12
Level 3

Hi 

I am not sure what you are trying to do exactly, but I find beforestore or afterstore do those jobs perfectly

Bucklash

248 Posts
Giuse
7 years ago
11
Level 4

Hi Bucklash, can you adsive on how , with beforeRender plugin, to fill a field C with concatenation of field A and B so that when the user fills field A and B, automatically field C is filled with "A-B" but the user is able to change this proposed field before hitting "save"? We need a script triggered by an event?
thanks!

Giuse

1283 Posts
Bucklash
7 years ago
10
Level 5

Hi

None of this was tested, purely done from memory as not near computer for a while...

Part 1...

Concat stored values using beforerender

Example:

$fields['field_c']->form = $fields['field_a']->value . '-' . $fields['field_b']->value;
//
// or maybe a default value
//
$fields['field_c']->form = $fields['field_a']->defaultvalue . '-' . $fields['field_b']->defaultvalue;

Part 2... 

Concat value of 'field_a' and 'field_b' in real time and display in 'field-c'

Example:

$(".field-a, .field-b").change( function() {
    var fieldvalues = $(".field-a").val() + '-' + $(".field-b").val();
  
    $(".field-c").val(fieldvalues);
});
// '.field-n' needs to be changed to whatever it is you require

Links

A dodgy tutorial I made a while ago:

learning-beforerender-form-not-value

Also jQuery

val

Plus see available data for a field using beforerender

before-render-quickie

Bucklash

EDIT 4th January 2017 (though still from memory until near computer... I will update appropriately when able to):

I missed out that after 'part 2', the user will see the changes but that is not enough....

Part 3...

Store the new value in Seblod.

   

In a beforestore field I think I would do something like this:

$fields['field_c']->value = $fields['field_c']->form;

248 Posts
Giuse
7 years ago
8
Level 6

Thanks very much Bucklash, I made some tests with your suggestions but...:

  • (part 1): the concat with before render works as output in the form, but the output values in the form are not stored into the db... why?
  • (part 2): I get an error with your code:

    syntax error, unexpected '(', expecting variable (T_VARIABLE) or '{' or '$'

do I need to fix something?
thanks again

Giuse

4229 Posts
Kadministrator
7 years ago
7
Level 7

for 1 - you need to set form to html rendering of the field. Bucklashes tutorial is not correct on this, values are not enough. Put some character like # as default value in your concatenated field. Then do something like this:

$newValue = $fields['field_a']->value . '-' . $fields['field_b']->value;
$fields['field_c']->form = str_replace( '#', $newValue, $fields['field_c']->form );

This will replace # from fields rendered html with your concatenated value.part 2 is javascript code not php, you need to put it in javascript code field.

248 Posts
Giuse
7 years ago
6
Level 8

Thanks Klas, I will try soon for part 1 - I think it's important to update Bucklash, or I am available to do it.

For part 2, I put Bucklash code into a JS field (of Code Pack plugin, of course changing the name of fields) but no effect: I type something into fieldA or fieldB, change cursor focus and fieldC is left as is and not automatically updated.... what's missing?

cheers and thanks

Giuse

4229 Posts
Kadministrator
7 years ago
5
Level 9

You will need to use javascript debugger and see what is happening. But if you do 1 you don't need 2.

248 Posts
Giuse
7 years ago
4
Level 10

Hi, method 1 does not allow realtime update of fieldC when fieldA and/or fieldB are changed, that is what I need method 2 and why I thought it was possible with computational rules.

I set the JS field with Behavior = DocumentReady and enabled chrome console but no error is thrown, but also nothing happens in fieldC when I change A or B. Is bucklash syntax correct and enough to modify realtime fieldC

$(".field-c").val(fieldvalues);

x?

thanks

Giuse

PS. may I ask for a fix in computational format to preserve field order when concatenating and an enhancement to include custom strings in concatenation? I could trace in github

4229 Posts
Kadministrator
7 years ago
3
Level 11

Hi,

function looks correct to me. Create a javacript breakpoint inside function to see if event gets triggered when values are changed.

248 Posts
Giuse
7 years ago
2
Level 12

Hi, I enabled breakpoint and the function is invoked just when the page is loaded in the browser, simply not invoked when the fields are changed. I also changed field behavior from "DocumentReady" to "Standard" but same effect, with "Raw" a javascript error is thrown ($ is not a function).

Is this the correct way of using a Seblod JS field?

$(".field-a, .field-b").change( function() {...});

Thanks again

Giuse

4229 Posts
Kadministrator
7 years ago
1
Level 13

Use raw mode , your jQuery library is probably in compatibility mode so you need to replace $ with jQuery

248 Posts
Giuse
7 years ago
0
Level 14

Actually after tests I found that the syntax must changed: from  $(".field")  to  $("#field"). Now the event is triggered also with DocumentReady behavior, but again values are shown but not saved: so also here with javascript just   $("#field").val(value)  is not enough and the form must be changed? The $("#field").form(value) seems not to be a proper instruction. 

At the end I can write a tutorial on this for other users..

cheers and thanks

Giuse

248 Posts
Giuse
7 years ago
0
Level 6

Hi Bucklash, I am using the javascript field plugin to show realtime the concatenation string while the user changes the two fields, using the change event as you suggested. It works using #field as selector (and not the .field class selector), but when I set the variation of the combined field to readOnly, it does not save values into the DB, white it does save the value if variation is set to default; the problem is that with default the user can modify the concatenation so I need the readonly. Something else is needed?

thanks a lot

Giuse

4229 Posts
Kadministrator
7 years ago
6
Level 1

jQuery val is correct way to set value of input fields

http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_html_set

But if you use script like select2 or chosen you will also need to trigger change event like this

$("#field").val(value).change();

Regarding selectors, # is to target field id, . is to target field's class, with Seblod both are set on fields, you just need to use the right one.

https://api.jquery.com/category/selectors/

This is my last post in this thread, if it still does not work I suggest you usehttps://www.seblod.com/store/services

248 Posts
Giuse
7 years ago
5
Level 2

Ok, I wanted to highlight for other users what I discovered with tests: change events in JS by default should use # as selector for Seblod fields - Seblod sets also classes but they do not identify uniquely fields unless adding custom classes, that's why my Before Render field didn't work at the beginning (I was using a trigger on class as selector but using field names as per Bucklash example) and the JS field behavior can be DocumentReady.

My final problem is saving values filled by Before Render and is not related to knowing JS: it works when variation is default, while if variation is Readonly => values are changes on the page but they are not saved, while if the new values are set by computational rules, readonly variation allows saving the new values.

I'll experiment by myself to find a possible workaround. Thanks for your support in this long thread.

Giuse

4229 Posts
Kadministrator
7 years ago
4
Level 3

This is security feature of Seblod - read-only value can't be modified client side. If you need to prevent users from modifying it your only option is server side, beforeStore should be the right event - just concatenate  and always overwrite your concatenated field value. So in your case you will probably need both - client side to show values and server side for actual storing.

248 Posts
Giuse
7 years ago
3
Level 4

That's clear now. Thanks as lot, thread marked as solved.

cheers

Giuse

248 Posts
Giuse
7 years ago
2
Level 5

I also opened an issue on Github since computational rules with string concatenation (that started this thread) has a problem for which order of fields are not respected: regardless of the order requested, the concatenation is done using the order of fields as they appear in the content type.

cheers

Giuse

67 Posts
Bonitto
7 years ago
1
Level 6

I agree that it would be great if SEBLOD supported some for of value separator and proper ordering of the fields in the Computation Concat field 

Guise can you write up exactly what modfications you had to make in order to solve this issue?

248 Posts
Giuse
7 years ago
0
Level 7

Hi, currently there is no way of using computational format to build a minimally complex field, since order is not preserved - as I wrote, I opened an issue on github since there is a bug for that. Also separators are quite cumbersome to use (you need to add a new field for every separator that you need).

So, waiting for fixing and enhancing computational format (that would be quite useful), for the moment I ended up adding a javascript code field to build the composite value on the browser of the user and another field (a php before store code field) to redo the construction of the value on the server, before storing the compite value into the DB.

If you do not need to show the value on the browser, you just need the second field (Before Store).

If you want to leave the user to modify the proposed value, you don't put "readonly" to the calculated field and do not need the second field, that is mandatory if the calculated field is set as readonly.

Hope to have clarified the alternatives.

Giuse

Get a VIP membership