Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Conditional validation

    I'm having a problem using the validators in a listGrid.
    In my listgrid there is a dropDown ("Yes" or "No"), when "yes" is selected 2 other fields in the listGrid should become required.
    Problem is that the validator on these fields is not called when the fields remain unchanged.
    So setting a requiredIf validator or conditional validator that makes the field required when "yes" is selected has no effect.
    Is there a possibility to immediatly validate these 2 text fields when the dropdown is changed?

    #2
    Calling markForRedraw() on the form will evaluated requiredIf and other conditions.

    Comment


      #3
      Not A Form

      It's not about a Form the guy was talking about.
      It was about a ListGrid.

      How do you get a reference to the correct row that is being edited from inside the expression field of the requiredIf validator?
      Some value on the same row will then determine wheiter the value in another column is required.

      Comment


        #4
        You can enable validateByCell() for more immediate validation.

        grid.getEditRow() will tell you the current edit row.

        Comment


          #5
          working example

          I'd like to post a working example on this forum, so other users can use this as well.
          Just one remark: the stopIfFalse attribute does not work well with a requiredIf validator. It still continues the other validations even if the field is not required.

          Is there a way to make the stopIfFalse work?

          Example of the conditional validator:
          Code:
          {name:"ppdOpen", width:40},
          {name:"ppdFrom", 
             validators:	[ {type:"requiredIf" ,
                  	   expression: 
                              function (item, validator, value) {
                  		var rowNum = colourCombinationGrid.getEditRow();
                  		record = colourCombinationGrid.getEditedRecord(rowNum);
          	        	if(record.ppdOpen == 'true' && (value == '' || value == null)){
              	    			return true;
                                  }else{
                  	                return false;
                  		}
                  	     },
                  	    stopIfFalse: true
                             },

          Comment


            #6
            By our tests this is working fine...just to be clear, stopIfFalse on a requiredIf means 'stop if the field is required and there is no value present', and not 'stop if the expression function returns false'. Also, in your code example, the blank value test is not necessary. Heres some sample code:
            Code:
            countryData = [
            
            {
                continent:"Asia",
                countryName:"Japan",
                countryCode:"JA",
                area:377835,
                population:127463611,
                gdp:4018.0,
                government:"constitutional monarchy with parliamentary government",
                government_desc:1,
                capital:"Tokyo",
                member_g8:true,
                article:"http://en.wikipedia.org/wiki/Japan"
            },
            {
                continent:"Asia",
                countryName:"India",
                countryCode:"IN",
                area:3287590,
                population:1095351995,
                gdp:3611.0,
                independence:new Date(1947,7,15),
                government:"federal republic",
                government_desc:2,
                capital:"New Delhi",
                member_g8:false,
                article:"http://en.wikipedia.org/wiki/India"
            }
            ]
            
            isc.ListGrid.create({
                ID: "countryList",
                width:500, height:224, alternateRecordStyles:true,
                data: countryData,
                canEdit: true,
                fields:[
                    {name:"countryCode", title:"Flag", width:50, type:"image", imageURLPrefix:"flags/16/", imageURLSuffix:".png"},
                    {name:"countryName", title:"Country",
                        validators:	[ 
                            {type:"requiredIf" ,
                             expression: function (item, validator, value) {
                                isc.logWarn('checking first requiredIf');
                                var rowNum = countryList.getEditRow();
                                record = countryList.getEditedRecord(rowNum);
                                if(record.continent == 'Asia' ){
                                    return true;
                                }else{
                                    return false;
                                }
                             },
                             stopIfFalse: true
                            },
                            {type:"isOneOf",
                             list:["China", "Japan", "India", "Russia"]
                                
                            },
                            {type:"requiredIf",
                             expression: function (item, validator, value) {
                                 isc.logWarn('second requiredIf');
                                 return false;
                             }
                                
                            }
                        ]
                    },
                    {name:"independence", title:"indep", type:"date"},
                    {name:"continent", title:"Continent"}
                ],
                canReorderFields: true
            })

            Comment

            Working...
            X