Announcement

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

    REST length validator

    Hello.

    DataSource definition
    Code:
    <DataSource ID="myDS" serverType="sql" tableName="myTable">
       <fields>
          <field name="myField" type="text" length="3"/>
       </fields>
    </DataSource>
    REST POST request
    Code:
    {
        dataSource:"myDS", 
        operationType:"add", 
        data:{
          myField: "abcd"
        }
    }
    In myTable I have a single column defined to take maximum 3 characters.
    All works well in the UI, because the FormItem I create related to the datasource field myField will accept only 3 characters at maximum.
    The problem is that if I send a REST request targeting that datasource, I will end up with an ugly DB exception, since I cannot store in the column more than 3 characters.
    To make sure this does not happen I need to change my field definition in the DS to

    Code:
    <DataSource ID="myDS" serverType="sql" tableName="myTable">
       <fields>
          <field name="myField" type="text" length="3">
                  <validators>
                       <validator type="lengthRange" max="3"/>
                  </validators>
           </field>
       </fields>
    </DataSource>
    The question is, couldn't smartgwt have such implicit validator when defining the length of the field?

    #2
    Yes, this makes sense for the REST endpoint as you describe (in the SmartGWT UI, the presence of length already creates enforcement).

    We'll look at adding this as a 5.1 or 6.0 enhancement.

    Comment


      #3
      We did some tests, but the problem could not be reproduced. Could you please test our code and see whether you obtain the same problem?.

      DataSource:
      Code:
      <DataSource
          ID="animals"
          serverType="sql"
          tableName="animals"
          testFileName="animals.data.xml"
      >
          <fields>
              <field name="commonName"      title="Animal"             type="text"/>
              <field name="scientificName"  title="Scientific Name"    type="text"  primaryKey="true"  required="true"/>
              <field name="lifeSpan"        title="Life Span"          type="integer"/>
              <field name="status"          title="Endangered Status"  type="text" length="255">
                  <valueMap>
                      <value>Threatened</value>
                      <value>Endangered</value>
                      <value>Not Endangered</value>
                      <value>Not currently listed</value>
                      <value>May become threatened</value>
                      <value>Protected</value>
                  </valueMap>
              </field>
              <field name="diet"            title="Diet"               type="text"/>
              <field name="information"     title="Interesting Facts"  type="text"  length="1000"/>
              <field name="picture"         title="Picture"            type="image" detail="true"
                     imageURLPrefix="/isomorphic/system/reference/inlineExamples/tiles/images/"/>
          </fields>
              
      </DataSource>
      Java code:
      Code:
      VLayout vl = new VLayout(20);
      		
      final RestDataSource result = new RestDataSource();
      result.setID("animals");
      result.setDataFormat(DSDataFormat.JSON);
      result.setDataURL("/isomorphic/RESTHandler");
      result.setDataProtocol(DSProtocol.POSTMESSAGE);
      DataSourceTextField commonName = new DataSourceTextField("commonName","commonName");
      DataSourceTextField scientificName = new DataSourceTextField("scientificName","scientificName");
      DataSourceIntegerField lifeSpan = new DataSourceIntegerField("lifeSpan","lifeSpan");
      DataSourceTextField status = new DataSourceTextField("status","status");
      DataSourceTextField diet = new DataSourceTextField("diet","diet");
      DataSourceTextField information = new DataSourceTextField("information","information");
      DataSourceTextField picture = new DataSourceTextField("picture","picture");
      OperationBinding fetch = new OperationBinding();
      fetch.setDataProtocol(DSProtocol.POSTMESSAGE);
      fetch.setOperationType(DSOperationType.FETCH);
      OperationBinding add = new OperationBinding();
      add.setDataProtocol(DSProtocol.POSTMESSAGE);
      add.setOperationType(DSOperationType.ADD);
      OperationBinding update = new OperationBinding();
      update.setDataProtocol(DSProtocol.POSTMESSAGE);
      update.setOperationType(DSOperationType.UPDATE);
      OperationBinding remove = new OperationBinding();
      remove.setDataProtocol(DSProtocol.POSTMESSAGE);
      remove.setOperationType(DSOperationType.REMOVE);
      result.setOperationBindings(fetch, add, update, remove);
      		
      result.setFields(commonName, scientificName, lifeSpan, status, diet, information, picture);
      		
      final DynamicForm form = new DynamicForm();
      form.setDataSource(result);
      		
      final ListGrid listGrid = new ListGrid();
      listGrid.setDataSource(result);
      listGrid.setWidth(500);
      listGrid.setHeight(200);
      listGrid.setAutoFetchData(true);
      listGrid.addRecordClickHandler(new RecordClickHandler() {
      
      	@Override
      	public void onRecordClick(RecordClickEvent event) {
      		form.editRecord(listGrid.getSelectedRecord());
      	}
      });
      
      IButton button = new IButton();
      button.setTitle("Guardar");
      button.setWidth(150);
      button.addClickHandler(new ClickHandler() {
      
      	@Override
      	public void onClick(ClickEvent arg0) {
      		form.saveData();
      	}
             	
      });
              
      vl.addMember(listGrid);
      vl.addMember(form);
      vl.addMember(button);
      		
      vl.draw();
      log:
      Code:
      === 2014-12-10 11:30:14,537 [l0-0] INFO  Validation - Validation error: [
          {
              status:{
                  errorMessage:"Must be no more than 255 characters long"
              }
          }
      ]
      
      === 2014-12-10 11:30:14,539 [l0-0] DEBUG RPCManager - Output to client: <SCRIPT>//'"]]>>isc_JSONResponseStart>>{"response":{"affectedRows":0,"errors":[{"status":{"errorMessage":"Must be no more than 255 characters long"}}],"invalidateCache":false,"isDSResponse":true,"operationType":"update","queueStatus":-1,"status":-4,"data":null}}//isc_JSONResponseEnd
      This code was tested with the 5.1d and 5.0p versions.

      Comment

      Working...
      X