EasyUI Forum
April 19, 2024, 12:54:25 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Serverside validation and editable datagrid  (Read 57268 times)
Onegin
Newbie
*
Posts: 11


View Profile
« on: July 14, 2011, 10:26:47 AM »

Is it possible to return false status to editable datagrid in server response?
When I create a new record, I use serverside validation for additional checks (e.g. duplicity of the item etc.)
I however didnt find anything in documentation, if there are any return values which save/update/destroy calls listen to.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: July 14, 2011, 07:44:58 PM »

Use the remote validatebox to do server side validation, the code looks like this:
Code:
{field:'name',title:'Your Title',width:100,
editor:{
  type:'validatebox',
  options:{
    required:true,
    validType:'url["dovalidate.php","name"]'
  }
}
}
The server side validate logic:
Code:
$name = $_REQUEST['name'];
//your validate code here
echo 'true'; // return true if validate successfully

Another way to do remote validation is to validate before end your editing.
Code:
var ed = $('#tt').datagrid('getEditor',{index:2,field:'name'}); // get the editor
var v = $(ed.target).val(); // get the field value
// do validate now
$.post('...',{name:v},function(result){
  if (result.success){
    $('#tt').datagrid('endEdit',2); // end editing
  }
});
Logged
Onegin
Newbie
*
Posts: 11


View Profile
« Reply #2 on: July 15, 2011, 06:39:56 AM »

The first way does not work with combobox. It always sends textValue to the server, not the fieldValue and I cant handle returned error message to show it to user.

The other way looks more flexible, but I was simply not able to find out, which event should I put it in...onBeforeEdit seems to be too early, onAfterEdit is already late...

I use $('#tt').edatagrid('saveRow'); call from toolbar button, I can probably do it this way:
handler:function(){
    //validation code

    $('#tt').edatagrid('saveRow');
}
but still I cant validate before new row is created automatically without user actually clicked on "Save" button. If user clicks on "Create" button, the currently edited row is sent to server without validation. And returning false, onSave event, seems not to stop row from being sent to server...I guess, it is needed to have onBeforeSave event.
« Last Edit: July 15, 2011, 06:44:25 AM by Onegin » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 on: July 15, 2011, 08:10:32 PM »

If use 'edatagrid' plugin, add 'onBeforeSave' event is a good idea.

Usage Example:
$('#tt').edatagrid({
  onBeforeSave:function(index){
    // do the row(index) validate
    return false; // return false to prevent editing action
  }
});
« Last Edit: July 15, 2011, 08:12:38 PM by stworthy » Logged
Onegin
Newbie
*
Posts: 11


View Profile
« Reply #4 on: July 16, 2011, 02:12:19 AM »

Indeed. Does it mean you are going to include it in some future release?
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #5 on: July 17, 2011, 06:38:45 PM »

The edatagrid plugin is an extension of standard library, see http://www.jeasyui.com/extension/edatagrid.php for more details.
Logged
Onegin
Newbie
*
Posts: 11


View Profile
« Reply #6 on: July 18, 2011, 02:02:20 AM »

Ah, it is already updated Smiley nice...

Anyways, I have now problem to retrieve data of all editors in a row inside onBeforeSave(), so I can sent it to server in an post array...
I tried:
Code:
onBeforeSave: function(index){
    $('#tt').edatagrid('endEdit', index);
    var changes = $('#tt').edatagrid('getChanges');
}

but it seems that call endEdit triggers saving of the data to the server regardless what is defined in
onBeforeSave
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #7 on: July 19, 2011, 12:16:29 AM »

Call 'endEdit' method will finish editing action and the row data will be posted to server. In this case, you can get all the editing field value and do some validation:

Code:
onBeforeSave:function(index){
var nameEditor = $('#tt').edatagrid('getEditor',{index:index,field:'name'});
var countryEditor = $('#tt').edatagrid('getEditor',{index:index,field:'country'});
var nameValue = $(nameEditor.target).val(); // get name field value
var countryValue = $(countryEditor.target).combobox('getValue'); // get country field value, notice that the editor type is combobox
// do validation about name and country field
return false; // valiate failure
}
Logged
Onegin
Newbie
*
Posts: 11


View Profile
« Reply #8 on: July 19, 2011, 04:42:03 AM »

Thank you very much for help and update.
$(nameEditor.target) was the tricky part...Smiley
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!