Commit 30181ab9 authored by thejoelinux's avatar thejoelinux

modal api / validation

parent 39a836cc
......@@ -30,10 +30,14 @@ class Esar_Category extends Record {
return $esar_category;
}
public function validate(&$errors) {
public function validate_input(&$errors) {
$v = new Valitron\Validator($_REQUEST);
$v->rule('required', ['name', 'label']);
$v->rule('alphaNum', ['name', 'label']);
$v->labels(array(
'name' => 'Le nom',
'label' => 'L\'etiquette'
));
if($v->validate()) {
$errors = null;
return true;
......
......@@ -2,6 +2,11 @@
class Record {
public function validate_input() {
trigger_error("Help me, i'm a poor object ".get_class($this)." with no validation", E_USER_WARNING);
return true;
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
......
......@@ -19,8 +19,8 @@ class User extends Record {
}
public static function fetch($id) {
$user = json_decode(file_get_contents($GLOBALS["saas_auth_url"].
"?o=users&i=".$id));
//$user = json_decode(file_get_contents($GLOBALS["saas_auth_url"].
// "?o=users&i=".$id));
......
......@@ -90,7 +90,7 @@ class AppController {
try {
$classname = $this->model;
$object = new $classname(0);
if(!$object->validate($errors)) {
if(!$object->validate_input($errors)) {
if($this->format == "json") {
header($_SERVER['SERVER_PROTOCOL']." 422 Unprocessable entity", true, 422);
echo json_encode($errors);
......@@ -145,8 +145,20 @@ class AppController {
$classname = $this->model;
$object = $classname::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($object->id != 0) {
if(!$object->validate_input($errors)) {
if($this->format == "json") {
header($_SERVER['SERVER_PROTOCOL']." 422 Unprocessable entity", true, 422);
echo json_encode($errors);
exit();
}
}
if($object->update()) {
$this->set_message("Modification enregistrée");
if($this->format == "json") {
header($_SERVER['SERVER_PROTOCOL']." 201 Created", true, 201);
echo json_encode($object);
exit();
}
}
$objects = array();
$classname::fetch_all($objects);
......
......@@ -14,7 +14,7 @@
<input type="text" id="name" name="name" class="form-control" value="{{ object.name }}"/>
<span id="help-name" class="help-block" style="display: none"></span>
</div>
<label class="control-label col-sm-2" for="label">label</label>
<label class="control-label col-sm-2" for="label">Etiquette</label>
<div class="col-sm-4">
<input type="text" id="label" name="label" class="form-control" value="{{ object.label }}"/>
<span id="help-label" class="help-block" style="display: none"></span>
......
......@@ -13,7 +13,7 @@
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{% if object.id != 0 %}
<input type="hidden" id="modal_object_id" value="{{ object.id }}">
<input type="submit" class="btn btn-success" id="save_button" value="Enregistrer les changements">
<input type="button" class="btn btn-success" id="save_button" value="Enregistrer les changements">
<input type="button" class="btn btn-danger" id="delete_button" value="Supprimer">
{% else %}
<input type="hidden" id="modal_object_id" value="0">
......@@ -25,43 +25,42 @@
<script>
// buttons events
$('#save_button').click(function(){
if($('#modal_object_id').val() == 0) {
{% block javascript_fields %}{% endblock %}
var post_data = { o: $('#o').val(), a: 'create' };
$.each(fields, function (key, val) {
post_data[val] = $('#'+val).val();
// and clean the error messages
$('#'+val).parent().removeClass('has-error');
$('#help-'+val).html('').hide();
});
{% block javascript_fields %}{% endblock %}
var post_data = { o: $('#o').val() };
if ($('#modal_object_id').val() == 0) {
post_data['a'] = 'create';
} else {
post_data['a'] = 'update';
post_data['i'] = $('#modal_object_id').val();
}
$.each(fields, function (key, val) {
post_data[val] = $('#'+val).val();
// and clean the error messages
$('#'+val).parent().removeClass('has-error');
$('#help-'+val).html('').hide();
});
$.post('api.php', post_data)
.done(function( data ) {
$('#created_ok').show();
window.setTimeout(1000,
location.href='index.php?o=' + $('#o').val() + '&a=list');
})
.fail(function(data) {
$.each(data.responseJSON, function(key, val) {
// add the class error to the field
$('#' + key).parent().addClass('has-error');
// display reasons
var helptext = '<ul>';
// format val
$.each(val, function(key2, val2) {
helptext += '<li>' + val2 + '</li>';
});
$('#help-' + key).html(helptext + '</ul>').show();
$.post('api.php', post_data)
.done(function( data ) {
$('#created_ok').show();
//window.setTimeout(1000,
// location.href='index.php?o=' + $('#o').val() + '&a=list');
})
.fail(function(data) {
$.each(data.responseJSON, function(key, val) {
// add the class error to the field
$('#' + key).parent().addClass('has-error');
// display reasons
var helptext = '<ul>';
// format val
$.each(val, function(key2, val2) {
helptext += '<li>' + val2 + '</li>';
});
return false;
$('#help-' + key).html(helptext + '</ul>').show();
});
return true;
} else {
$('#i').val($('#modal_object_id').val());
$('#a').val('update');
}
document.defaultform.submit();
return false;
});
return true;
});
$('#delete_button').click(function(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment