Skip to content

Validation

Validation

JS Validation

You can specify the validation rules required on each column. Possible options are mentioned below

$col["editrules"] = array("required"=>true);
$col["editrules"] = array("number"=>true);
$col["editrules"] = array("email"=>true);
$col["editrules"] = array("date"=>true);
$col["editrules"] = array("minValue"=>5, "maxValue"=>10);
$col["editrules"] = array("url"=>true);

The date validation will check input against format specified in datefmt option, see datefmt.

Custom JS Validation

For custom validation function (be it ajax remote checking or complex regex), Follow following steps. For e.g. to check certain column value must be greater than 100:

Step1: Define custom_func property for JS function

$col = array();
$col["title"] = "Date";
$col["name"] = "invdate";
$col["width"] = "50";
$col["editable"] = true;
$col["editrules"] = array("custom"=>true,"custom_func"=>"function(val,label){return my_validation(val,label);}");
$cols[] = $col;

Step2: Define JS callback function

<script>
function my_validation(value,label)
{
    if (value > 100)
        return [true,""];
    else
        return [false,label+": Should be greater than 100"];
}
</script>

To validate some field 'onblur' JS event, set it in 'editoptions' and define JS callback function like above.

$col["editoptions"] = array("onblur"=>"validate_onblur(this)");

Resources

Server Validation

Server Validation

If you want to validate that client does not already exist in database. Following code will prompt the ‘already exist’ message as data entry error.

Step1: Define on_insert event handler 'add_client' for server validation.

$e["on_insert"] = array("add_client", null, true);
$grid->set_events($e);

Step2: The helper function ‘phpgrid_error’ will display your server side validation message as error dialog.

function add_client($data)
{
    global $grid; // where $grid = new jqgrid(...);

    $check_sql = "SELECT count(*) as c from clients where LOWER(`name`) = '".strtolower($data["params"]["name"])."'";

    $rs = $grid->get_one($check_sql);

    if ($rs["c"] > 0)
        phpgrid_error("Client already exist in database");

    $grid->execute_query("INSERT INTO clients VALUES (null,'{$data["params"]["name"]}','{$data["params"]["gender"]}','{$data["params"]["company"]}')");
}

Resources

^ Top