[GIS] Problem with save strategy

geoserveropenlayers-2

Below is my code which i am using for modifying features and drawing new features.

saveStrategy = new OpenLayers.Strategy.Save();
saveStrategy.events.register('success', null, ChangesSuccess);
saveStrategy.events.register('fail', null, ChangesFailed);

wfs = new OpenLayers.Layer.Vector("Edit Vagmitt Polyline", {

    styleMap: StyleMap,
    eventListeners: {
        "featuresadded": ZoomToData
    },
    strategies: [new OpenLayers.Strategy.Fixed(), saveStrategy],
    protocol: new OpenLayers.Protocol.WFS({
        url: GEOSERVER_WFS,
        featureType: "new_Vagmitt_polyline",
        featureNS: "http://www.openplans.org/workspace",
        geometryName: "the_geom",
        srsName: "EPSG:900913",
        version: "1.1.0",
        extractAttributes: true,
        isBaseLayer: false,
        visibility: true
    })

});

new OpenLayers.Control.DrawFeature(wfs, OpenLayers.Handler.Path, {displayClass: 'lineButton', title: 'Draw new line feature', handlerOptions: {style: sty}});

new OpenLayers.Control.ModifyFeature(wfs, {title: 'Edit line feature'});
The code works fine for modifyFeature control, but its gives error on save strategy when i draw a new feature.

The changes are also saved back to Geoserver when i modify the feature (triggers ChangesSuccess). but in case of DrawFeature control, saveStrategy triggers ChangesFailed function.

I could not understand, why ModifyFeature works but DrawFeature does't work.
Can anybody explain, where is the problem ?

Best Answer

Probably you missed to set the feature's state to INSERT. Here is an example code:

var geom = new OpenLayers.Geometry.Point(x, y);
var feature = new OpenLayers.Feature.Vector(geom);
feature.state = OpenLayers.State.INSERT;

See this OpenGeo workshop for a complete example:

http://workshops.opengeo.org/geoext/wfs/wfst.html