Skip to content

Commit f0e2dda

Browse files
jacintocapoteacouch
authored andcommitted
Added some basic validation to json format
1 parent c3f388f commit f0e2dda

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

leaflet_widget.module

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,28 @@ function leaflet_widget_widget_validate($element, &$form_state) {
189189
$geophp = geophp_load();
190190
if (!$geophp) return FALSE;
191191

192+
//Validate json type is valid.
192193
$geojson = json_decode($element['wkt']['#value']);
194+
$types = array('FeatureCollection', 'GeometryCollection', 'Feature', 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon');
195+
193196
$results = array();
194197
if ($geojson) {
198+
//Validate type
199+
if (!in_array($geojson->type, $types)) {
200+
form_set_error($element['wkt']['#name'], 'Please enter valid GeoJSON');
201+
return FALSE;
202+
}
203+
204+
//validate structure
205+
try {
206+
$geometry = geoPHP::load($element['wkt']['#value'], 'json');
207+
$geometry->out('wkt');
208+
}
209+
catch (Exception $e) {
210+
form_set_error($element['wkt']['#name'], 'Please enter valid GeoJSON');
211+
return FALSE;
212+
}
213+
195214
if ($geojson->type === 'FeatureCollection') {
196215
foreach ($geojson->features as $feature) {
197216
$results[] = array_merge($results, leaflet_widget_process_geojson($feature));

0 commit comments

Comments
 (0)