FieldTextInput and FieldNumber methods are not giving single digit value to starts with zero

57 views
Skip to first unread message

Sahithi Mangena

unread,
Feb 25, 2021, 2:32:09 AM2/25/21
to blo...@googlegroups.com
Hello,
We are using FieldTextInput to get user value. Previously we used to get a single digit value starting with 0. But in the updated blockly 3.2 vesion we are not getting like the previous one. 

this.appendDummyInput("RHS_INPUT")
      .appendField(new Blockly.FieldTextInput("value"), "value");

var value = block.getFieldvalue('value');

We basically need this for the input date of birth and in the  month field we need the month to start with 0. like February is 02 .

We tried using FieldNumber also but not giving the values like before.
Can you please help on this. Do we need to change any method?

Regards,
Sahithi

Beka Westberg

unread,
Feb 25, 2021, 4:42:24 PM2/25/21
to blo...@googlegroups.com
Hello Sahithi :D

> Previously we used to get a single digit value starting with 0.

Hmmm, could you explain a little bit more about this? Are you saying that the field no longer allows you to enter values like "02"? Or are you saying that the field used to correct values like "2" to "02" (possiblying using a validator)? In either case, I'm not sure what could be causing that. But with a bit more information I'm sure we can figure it out :D If you could provide a video of the old behavior vs the new behavior that might help too!

But in general, if you want to make sure user input from fields follows certain rules, you should use a validator for that. If the validator receives invalid input, you can have it return `null` to ignore the input, or return a corrected value which will show up in the field. You could also add the validation into your code generator, but that makes it harder for the user to correct :/

Sorry i can't be more help :/
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CAKjwcik%2B-50qahFrc%2BWAa-x-wCCQ8hUbfmVkyxos_7TyJ_QhfA%40mail.gmail.com.

Sahithi Mangena

unread,
Feb 27, 2021, 10:01:55 AM2/27/21
to blo...@googlegroups.com
Hi beka,

Screenshot 2021-02-27 at 8.24.55 PM.png

Here i am attaching the screen shot. So if you see in the console the value is showing like "6", but previously blockly used to give single digit values like "06" like we are updated blockly from 1 version to 3. In the new version it is not giving the values for single digit to start with zero. 
So we need the value it self to come as "06". To get like this do we need to add any validators on the value field.

this.appendDummyInput("RHS_INPUT")
      .appendField(new Blockly.FieldTextInput("value"), "value");

var value = block.getFieldvalue('value');

Please give some suggestions on this.

Thanks and regards
Sahithi.

Beka Westberg

unread,
Feb 27, 2021, 10:35:46 AM2/27/21
to blo...@googlegroups.com
Hello again Sahithi,

> So if you see in the console the value is showing like "6", but previously blockly used to give single digit values like "06" like we are updated blockly from 1 version to 3.

Weird! I've never seen behavior like that before. Do you maintain a fork of Blockly? Maybe you used to have some custom code in there that got lost during the upgrade.

> To get like this do we need to add any validators on the value field.

I think you have two options:
1) Create a validator that prepends 0s to single digit numbers. The problem is this will also change the value that is visible to the user.
2) Modify your code generators to prepend 0s to single digit numbers.

For 1 you would need to add code like:
```
Blockly.Blocks['my_block'] = {
  init: function() {
    this.appendDummyInput("RHS_INPUT")
        .appendField(new Blockly.FieldTextInput("value", this.validate), "value");
  },

  validate: function(val) {
    if (!isNaN(val) && val.length == 1) {  // If the value is a single digit number.
      return '0' + val;
    }
    return val;  // Not necessary, but makes the validator clear.
  }
}
```

For 2 you would need to add code like:
```
Blockly.JavaScript['my_block'] = function(block) {
  var text_value = block.getFieldValue('NAME');
  if (!isNaN(text_value) && text_value.length == 1) {  // If the value is a single digit number.
    text_value = '0' + text_value;
  }
  /// assembled and return code string...
};
```

Note that this code hasn't been tested! But I think it should work.

I hope that helps :D
--Beka


Reply all
Reply to author
Forward
0 new messages