how to validate a variable field which can only accept predefined variables?

386 views
Skip to first unread message

Vihang Kale

unread,
Jan 2, 2021, 2:20:39 AM1/2/21
to Blockly
I referred the docs and tried to apply the same but I am not getting the results.I have provided the code below so kidnly refer the code and help me with this problem.Thank you

html code

<block type = "variables_validate_predefined">
      <field name = "Intel" variable type="integer">Intel</field> 
      <field name = "AMD" variable type="integer">AMD</field> 
      <field name = "Snapdragon" variable type="integer">Snapdragon</field> 
    </block>  

//block definition
Blockly.Blocks['variables_validate_predefined'] = {
  init: function() {
        this.appendValueInput("predefined_validation")
        .setCheck(null)
        .appendField(new Blockly.FieldVariable("Intel", this.validate), "Intel") 
        .appendField(new Blockly.FieldVariable("AMD", ), "AMD") 
        .appendField(new Blockly.FieldVariable("Snapdragon", ), "Snapdragon") 
    this.setOutput(true, null);
    this.setColour(120);
 this.setTooltip("predefined");
 this.setHelpUrl("");
  },

 validate:function(newValue) {
  var validIds = ['Intel', 'Snapdragon', 'Exynos'];
  if (validIds.indexOf(newValue) == -1) {
    return null;
  }
  return newValue;
 },
};


//generator snub
Blockly.JavaScript['variables_validate_predefined'] = function(block) {
  var variable_Intel = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('Intel'), Blockly.Variables.NAME_TYPE);
  var value_Intel = Blockly.JavaScript.valueToCode(block, 'Intel', Blockly.JavaScript.ORDER_ATOMIC);
  var variable_AMD = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('AMD'), Blockly.Variables.NAME_TYPE);
  var value_AMD = Blockly.JavaScript.valueToCode(block, 'AMD', Blockly.JavaScript.ORDER_ATOMIC);
  var variable_Snapdragon = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('Snapdragon'), Blockly.Variables.NAME_TYPE);
  var value_Snapdragon = Blockly.JavaScript.valueToCode(block, 'Snapdragon', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '...';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};



Beka Westberg

unread,
Jan 2, 2021, 10:47:48 AM1/2/21
to blo...@googlegroups.com
Hello,

> I referred the docs and tried to apply the same but I am not getting the results.

Could you expand a bit more on what results you are getting, and how they are different from the results you want?

One thing that stands out to me is that you are using three different variable fields, but only one of them has a validator attached:
```
Blockly.Blocks['variables_validate_predefined'] = {
  init: function() {
        this.appendValueInput("predefined_validation")
        .setCheck(null)
        // This is the only field with a validator attached.

        .appendField(new Blockly.FieldVariable("Intel", this.validate), "Intel")
        // The below two fields have an extra comma, are they missing a validator?

        .appendField(new Blockly.FieldVariable("AMD", ), "AMD")
        .appendField(new Blockly.FieldVariable("Snapdragon", ), "Snapdragon")
    this.setOutput(true, null);
    this.setColour(120);
    this.setTooltip("predefined");
    this.setHelpUrl("");
  },

 validate:function(newValue) {
  var validIds = ['Intel', 'Snapdragon', 'Exynos'];
  if (validIds.indexOf(newValue) == -1) {
    return null;
  }
  return newValue;
 },
};
```

Is this what you intended?

Another thing I notice is that your variable type xml property should be all one word:
```
   <block type = "variables_validate_predefined">
      <!-- These three lines changed -->
      <field name = "Intel" variabletype="integer">Intel</field>
      <field name = "AMD" variabletype="integer">AMD</field>
      <field name = "Snapdragon" variabletype="integer">Snapdragon</field>
    </block>  
```

And if you do want your variables to be typed, you should pass that to the field constructor. For example:
```
new BlocklyFieldVariable('Intel', this.validate, ['Integer'], 'Integer');
```

But your validate function does look to be correct!

I hope that gives you some place to start,
--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/53f54c79-26e8-4a56-b70d-3c8a570ed5e2n%40googlegroups.com.
Message has been deleted

Vihang Kale

unread,
Jan 2, 2021, 1:09:26 PM1/2/21
to Blockly
Yes, actually I want to check the validation for one variable for now.So the validation should only allow 'Intel', 'Snapdragon', 'Exynos'(which doesn't exist but still I am using for testing purpose) to be selected or to be used from the dropdown but the actual result is that I can only use or select Intel and rest of the variables can't be selected.
//corrections
- no those fields weren't intended for validations,I forgot to remove the commas and also thanks for pointing out the correction regarding variable type.

Thanks Beka for the help!

Abby Schmiedt

unread,
Jan 5, 2021, 8:11:04 PM1/5/21
to Blockly
Hello!

From what I can tell you will want something like below: 
```
validate:function(newValue) {
// newValue is the id of the variable, not the value of the variable.
var variable = this.getSourceBlock().workspace.getVariableById(newValue);
// Once we get the variable we can use the name.
var variableName = variable.name;

var validIds = ['Intel', 'Snapdragon', 'Exynos'];
if (validIds.indexOf(variableName) == -1) {

return null;
}
return newValue;
},
```
The problem was 'newValue' is actually the id of the variable, and not the name of the variable.

Hope this helps!
Abby

Vihang Kale

unread,
Jan 6, 2021, 5:10:16 AM1/6/21
to Blockly
Thanks for clarifying my doubt Abby!
Reply all
Reply to author
Forward
0 new messages