Announcement

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

    #16
    For those who still have problem with record/cell validation and all the submitted suggestions doesn't fit, here is a work around:

    Code:
    private boolean validateRecordRequiredFields(final ListGridRecord record, final List<ListGridField> requiredFields)
    	{
    		int size = requiredFields.size();
    
    		for (int i = 0; i < size; ++i)
    		{
    			ListGridField lgf = requiredFields.get(i);
    			String fieldName = lgf.getName();
    
    			if (record.getAttribute(fieldName) == null)
    			{
    				Integer recordIdx = lgTable.getRecordIndex(record);
    
    				lgTable.setEditValue(recordIdx, fieldName, "null");
    				lgTable.validateCell(recordIdx, fieldName);
    				lgTable.setEditValue(recordIdx, fieldName, "");
    				lgTable.validateCell(recordIdx, fieldName);
    
    				return false;
    			}
    		}
    
    		return true;
    	}
    I assume that this "problem" is not a limitation of the framework but it will be handy if a "forceValidateRow()" or something else could be available in one of the next releases of smartgwt.

    Regards,
    Cavaleiro.
    Last edited by Cavaleiro; 18 Jan 2012, 04:55.

    Comment


      #17
      Thank you Cavaleiro. It finally validates new records.

      Comment


        #18
        Cavaleiro's solution seems to validate null fields.

        I ran into the same problem reported in this topic: I have some data entered by the user via an edit box, and then I display it on ListGrid, so when I add it to the ListGrid it is effectively user-entered data, and needs to be validated, but the ListGrid does not validate it because it assumes that data added/updated via addData () / updateData () has already been validated.

        I tried using startEditing () /startEditingNew () / endEditing () with no luck:
        1. The first record of the ListGrid appeared to be blue in color
        2. The most recent record added would show edit boxes but no errors

        So I went back to basics and just added a method that I could simply call to validate all the data on the ListGrid after I load it. I added these methods to my ListGrid class which extends com.smartgwt.client.widgets.grid.ListGrid:
        Code:
          /**
           * Validates all rows/cells whether they were entered by the user or added via addData ();
           * Returns true if validation was successful (no errors encountered), false otherwise
           * See http://forums.smartclient.com/showthread.php?t=13223&page=2&highlight=listgrid+validate
           * @return boolean
           */
          public boolean forceValidate () {
            
            // iterate all rows of data
            boolean sucessFlag = true;
            final ListGridField[] listGridFieldArray = super.getFields ();
            final int recordCount = getRecordCount ();
            for (int rowIndex = 0; rowIndex < recordCount; rowIndex ++) {
              
              // iterate all fields for this row
              final Record record = super.getRecord (rowIndex);
              for (final ListGridField listGridField : listGridFieldArray) {
                
                // acquire current value
                final String fieldName = listGridField.getName ();
                final String value = record.getAttributeAsString (fieldName);
        
                // change the cell value to some base value
                super.setEditValue (rowIndex, fieldName, "abc123ABC456!!!");
                super.validateCell (rowIndex, fieldName);
                
                // set the cell value to the original value forcing ListGrid to detect a change, then validate it
                super.setEditValue (rowIndex, fieldName, value);
                final Boolean validateFlag = super.validateCell (rowIndex, fieldName);
                if (validateFlag != null && validateFlag.equals (Boolean.FALSE)) {
                  sucessFlag = false;
                }
              }
            }
            
            // done
            return sucessFlag;
          }
          
          /**
           * Validates all rows/cells
           * Returns true if validation was successful (no errors encountered), false otherwise
           * Inline with other SmartGWT components, this validates ONLY data entered by the user
           * See forceValidate () if you want to validation independent of whether the user entered
           * the data, or it was handled programmatically via addData () / updateData ()
           * @return boolean
           */
          public boolean validate () {
            
            // iterate all rows
            boolean sucessFlag = true;
            final int recordCount = getRecordCount ();
            for (int rowIndex = 0; rowIndex < recordCount; rowIndex ++) {
              
              // validate this row
              final Boolean validateFlag = super.validateRow (rowIndex);
              if (validateFlag != null && validateFlag.equals (Boolean.FALSE)) {
                
                // validate for this row failed: track
                sucessFlag = false;
              }
            }
            
            // done
            return sucessFlag;
          }
        To use it:
        Code:
        final ListGrid listGrid = new ListGrid ();
        listGrid.setWhatever ();
        listGrid.addData (records...);
        listGrid.forceValidate ();
        I just coded these methods in the last few days so I am not sure that they are 100% correct, but they seem to work so far...

        Comment


          #19
          Hi
          Isomorphic and levi,

          im using levi's code which was there under validate button. But still im getting
          below error.
          when clicking on validate button new records adding with validations.
          But here i dont want to add new records.
          I just want to validate with existed records.

          Below is my code:

          int rowNum = secDBDeviceListGrid.getRowNumberStart();
          System.out.println("RowNrStart : " + rowNum);
          for(ListGridRecord rec : secDBDeviceListGrid.getRecords()){
          secDBDeviceListGrid.setEditValue(rowNum, "First", rec.getAttributeAsString("First"));
          secDBDeviceListGrid.setEditValue(rowNum, "Second", rec.getAttributeAsString("Second"));

          rowNum++;
          secDBDeviceListGrid.startEditingNew();
          }

          Here im not able find where i did mistake.

          Thanks in advance.

          Comment


            #20
            Hi levi,
            Earlier you faced some problem and solved.
            When clicking on validate button 2 records are addding.
            now im also having the same issue.
            I just want to know how to validate with existed records.
            validations are coming properly but new records are coming that is only
            my issue. can you help me to do this.

            Thanks in Advance

            Comment

            Working...
            X