Question T5164
Visible to All Users

Survey creator add property of type validator

created 4 years ago (modified 4 years ago)

Hello my awesome surveyjs team,

need a help/example to add a validators type property with my own choices

So the overall feature I am trying to build is ->
#1 - Loading a definition list from ajax (countries, regions, users etc master records and it's a custom property of type itemvalues)
#2 - Than once select a definition type, say 'countries' than it will load another custom property of type validators. Which should have values like text, order by, row counts etc. (in this one I am stuck and need urgent help)
#3 - Next if you choose say 'text' than it will load 3 new text box like the validation text selection is doing now(I think I can do this, once I manage to do the #2)
#4 - This setting will filter the list of definition that will be loading in the survey viewer
(the feature we are trying to build is: load any master record as a dropdown list type question also filter on text/order by/row count on top of that)

(i am following this documentation : Add Properties to creator doc)

Code
SurveyKo.Serializer.addProperty("selectbase", {       name'definitionFilters:validators',       default' ',       dependsOn: ["definitions""choiceType"],       visibleIffunction (obj{         var definitions = !!obj ? obj.definitions : null;         var choiceType = !!obj ? obj.choiceType : null;         if (definitions !== 'choose' && choiceType === 0) {           return true;         }         else {           return false;         }       },       // choices: [       //   { text: "Choose", value: -1 },       //   { text: "Text", value: 0 },       //   { text: "Order", value: 1 },       //   { text: "Top rows {row number}", value: 2 },       // ],       category: "New"     });

definitionFilteringFeature1.pngdefinitionFilteringFeature.png

Comments (2)

    Hello,
    What is the final goal? Do you want to restrict validators type for selectbase questions (checkbox, radiogroup, dropdown, imagepicker)?
    validators property type ignores "choices" property attribute.

    Thank you,
    Andrew
    SurveyJS Team

      Thanks for replying quicker than expected.

      The goal is to utilize the 'validators' type control to define my own selction items(mytext/order by/rowcount instead of expression/numeric/text/regex/email)

      And than on selection of any options(mytext/order by/rowcount) load/visible different properties(like the way validator does but this time my own custom properties).

      Answers approved by surveyjs Support

      created 4 years ago

      Hello,
      Here is the code that do the trick in our library. Unfortunately, you can't modify it for existing questions. We can add something for new version, for example fire a survey event.
      Did you create the needed validation classes?

      Thank you,
      Andrew
      SurveyJS Team

        Comments (2)

          Can you please point me out some plunker site or some code example about how to create custom validation classes also how can I show only those custom validator for this property only?

            We used to show how to create your own validation class as example, but since nobody use it, we remove this example. You can solve all your issues by using the expression validator now. You can easily create a custom function(s) and use it. Here is the example.

            Here is the code from the example that we have removed/changed:

            JavaScript
            var MyTextValidator = (function (_super{   Survey.__extends(MyTextValidator, _super);   function MyTextValidator({     _super.call(this);   }   MyTextValidator.prototype.getType = function ({     return "mytextvalidator";   };   MyTextValidator.prototype.validate = function (value, name{     if (value.indexOf("survey") < 0) {       //report an error       return new Survey.ValidatorResult(         null,         new Survey.CustomError(this.getErrorText(name))       );     }     //return Survey.ValidatorResult object if you want to correct the entered value     // return new Survey.ValidatorResult(youCorrectedValue);     //return nothing if there is no any error.     return null;   };   //the default error text. It shows if user do not set the 'text' property   MyTextValidator.prototype.getDefaultErrorText = function (name{     return "You text should contains 'survey' word.";   };   return MyTextValidator; })(Survey.SurveyValidator); Survey.MyTextValidator = MyTextValidator; //add into survey Json metaData Survey.Serializer.addClass(   "mytextvalidator",   [],   function ({     return new MyTextValidator();   },   "surveyvalidator" );

            Than you can use it as:

            JavaScript
            validators: [{ type"mytextvalidator" }],

            Thank you,
            Andrew
            SurveyJS Team