Changeset 6446

Show
Ignore:
Timestamp:
11/16/10 19:48:02 (3 years ago)
Author:
jow
Message:

libs/web: add range(min,max) datatype validator

Location:
luci/trunk/libs/web
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/web/htdocs/luci-static/resources/cbi.js

    r6369 r6446  
    156156    { 
    157157        return (v.match(/^[a-zA-Z0-9_]+$/) != null); 
     158    }, 
     159 
     160    'range': function(v, args) 
     161    { 
     162        var min = parseInt(args[0]); 
     163        var max = parseInt(args[1]); 
     164        var val = parseInt(v); 
     165 
     166        if (!isNaN(min) && !isNaN(max) && !isNaN(val)) 
     167            return ((val >= min) && (val <= max)); 
     168 
     169        return false; 
    158170    } 
    159171}; 
     
    635647{ 
    636648    var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid; 
     649    var vargs; 
     650 
     651    if( type.match(/^(\w+)\(([^\(\)]+)\)/) ) 
     652    { 
     653        type  = RegExp.$1; 
     654        vargs = RegExp.$2.split(/\s*,\s*/); 
     655    } 
     656 
    637657    var vldcb = cbi_validators[type]; 
    638658 
     
    650670                    ? field.options[field.options.selectedIndex].value : field.value; 
    651671 
    652                 if( !(((value.length == 0) && optional) || vldcb(value)) ) 
     672                if( !(((value.length == 0) && optional) || vldcb(value, vargs)) ) 
    653673                { 
    654674                    // invalid 
  • luci/trunk/libs/web/luasrc/cbi.lua

    r6355 r6446  
    13591359-- Validate the form value 
    13601360function AbstractValue.validate(self, value) 
    1361     if self.datatype and value and datatypes[self.datatype] then 
    1362         if type(value) == "table" then 
    1363             local v 
    1364             for _, v in ipairs(value) do 
    1365                 if v and #v > 0 and not datatypes[self.datatype](v) then 
     1361    if self.datatype and value then 
     1362        local args = { } 
     1363        local dt, ar = self.datatype:match("^(%w+)%(([^%(%)]+)%)") 
     1364 
     1365        if dt and ar then 
     1366            local a 
     1367            for a in ar:gmatch("[^%s,]+") do 
     1368                args[#args+1] = a 
     1369            end 
     1370        else 
     1371            dt = self.datatype 
     1372        end 
     1373 
     1374        if dt and datatypes[dt] then 
     1375            if type(value) == "table" then 
     1376                local v 
     1377                for _, v in ipairs(value) do 
     1378                    if v and #v > 0 and not datatypes[dt](v, unpack(args)) then 
     1379                        return nil 
     1380                    end 
     1381                end 
     1382            else 
     1383                if not datatypes[dt](value, unpack(args)) then 
    13661384                    return nil 
    13671385                end 
    13681386            end 
    1369         else 
    1370             if not datatypes[self.datatype](value) then 
    1371                 return nil 
    1372             end 
    1373         end 
    1374     end 
     1387        end 
     1388    end 
     1389 
    13751390    return value 
    13761391end 
  • luci/trunk/libs/web/luasrc/cbi/datatypes.lua

    r6370 r6446  
    209209    return (val:match("^[a-zA-Z0-9_]+$") ~= nil) 
    210210end 
     211 
     212function range(val, min, max) 
     213    val = tonumber(val) 
     214    min = tonumber(min) 
     215    max = tonumber(max) 
     216 
     217    if val ~= nil and min ~= nil and max ~= nil then 
     218        return ((val >= min) and (val <= max)) 
     219    end 
     220 
     221    return false 
     222end