Announcement

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

    Form validation against Datasource

    Hi,
    I'm facing one problem in validating form when form bound with datasource.
    I have a grid and form which are bound to same datasource. Form is used to insert new record to grid and update the datasource.

    For datasource, i'm using GwtRPCDatasource.

    I want to do primary key validation. Please find my case below:

    i have a record with Project Name and Project Status fields. I've set Project Name as primary key at datasource level. The same datasource i set to form. At form level, i've set required(true) to project name field. i've added submit item to form. When i click submit, its showing required validation errors. After i correcting those errors, it should show error for primary key violation if i give the project name which already exists. But its not giving any error and submitting the form successfully.
    Please find sample code below:

    1) ProjectDS
    ===================
    public class ProjectDS extends GwtRpcDataSource{
    public ProjectDS(String id) {
    setID(id);
    DataSourceTextField projectNameField = new DataSourceTextField(FIELD_PROJECT_NAME, "Project Name");
    projectNameField.setPrimaryKey(true);
    DataSourceTextField statusField=new DataSourceTextField(FIELD_PROJECT_STATUS,"Status");
    setFields(projectNameField,statusField);

    }

    2)MyForm
    =============

    final DynamicForm form=new DynamicForm();
    form.setDataSource(projectDS);

    TextItem projectNameItem=new TextItem(ProjectDS.FIELD_PROJECT_NAME,"Project Name");

    projectNameItem.setRequired(true);
    projectNameItem.setTooltip("Project Name");
    SubmitItem btnSubmit=new SubmitItem();

    form.setItems(projectNameItem,btnSubmit);
    ........
    ..............
    ...........


    Please tell me how to do primary key violation validation at form?

    #2
    You will need server help to do that because the client cannot know what values already exist. Using SGWT Pro or better, just add an isUnique validator and you are set (assuming you are using a db for the back end).

    If you are managing all of the data client-side in a grid, you can write your own validator to check for uniqueness against the grid records.

    Comment


      #3
      Hi david,
      Thanks for the reply. I'm loading the data into grid by setting datasource to grid while loading the page. So grid contains all records and when i want to add new one, it should check uniqueness as i set already projectname as primarykey to datasource. I've tried with what you said using UNIQUENESS. But no luck. Its not working.

      Validator prime=new Validator();
      prime.setType(ValidatorType.ISUNIQUE);
      projectNameField.setValidators(prime);

      Am i missing anything?

      Comment


        #4
        Since you are not using the SmartGWT server framework, you will have to implement a custom validator to check for uniqueness yourself - this is not a built-in feature client-side.

        The code you have would work perfectly with SmartGWT Pro and the server framework against a SQL db no matter how many records exist. One of the advantages of the licensed versions.

        Comment


          #5
          Ok. Thanks for the info. Can you please provide me an example of defining custom validators for a datasource or provide me any link which describes the same.

          Comment


            #6
            See the JavaDocs on CustomValidator for details. This will be defined on your projectNameField.

            Comment


              #7
              In my case, i need to validate projectName field against all existing records. That means, do i need to pass collection of all existing records as an attribute to custom validator or is there any way to get all records in Custom Validator?

              Comment


                #8
                You will need to provide the validator with a reference to your data to validate against. Try using setAttribute() with a reference to the grid. That way the validator can obtain the current list of records when called. Be careful of the situation of an add vs update and validation...

                Comment


                  #9
                  I found there is a method validateData() in DataSource. Currently i'm using GwtRpcDatasource implementation. I want to override this method. But this method signature is: void validateData()

                  This method is not taking any args and not returning any errors map.

                  How to pass fields to this method and return errors ,if any,if we override this method? Please suggest me.

                  Comment

                  Working...
                  X