Commit 165ea423 authored by thejoelinux's avatar thejoelinux

ajout d'un validateur et changement du mode de fonctionnement des modales

parent bb989a66
......@@ -29,4 +29,18 @@ class Esar_Category extends Record {
$GLOBALS["data"]->select($sql, $esar_category, "Esar_Category");
return $esar_category;
}
public function validate(&$errors) {
$v = new Valitron\Validator($_REQUEST);
$v->rule('required', ['name', 'label']);
$v->rule('alphaNum', ['name', 'label']);
if($v->validate()) {
$errors = null;
return true;
} else {
// Errors
$errors = $v->errors();
return false;
}
}
}
{
"require": {
"twig/twig": "~1.0",
"nette/mail": "^2.3"
"nette/mail": "^2.3",
"vlucas/valitron": "^1.2"
}
}
......@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "4075de6455546b6bbcb193216c2bda35",
"content-hash": "3d0b0ef0c5d39b5caf344aa0d41257c0",
"hash": "c5d451cd88aaa48b4d5033f3e0607b58",
"content-hash": "51ede62ccdbaafa5ac9770f8ae6174a0",
"packages": [
{
"name": "nette/mail",
......@@ -177,6 +177,52 @@
"templating"
],
"time": "2015-11-05 12:49:06"
},
{
"name": "vlucas/valitron",
"version": "v1.2.4",
"source": {
"type": "git",
"url": "https://github.com/vlucas/valitron.git",
"reference": "d2b63c611980bb804d17468bf0b3c8d4e28afbf5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/vlucas/valitron/zipball/d2b63c611980bb804d17468bf0b3c8d4e28afbf5",
"reference": "d2b63c611980bb804d17468bf0b3c8d4e28afbf5",
"shasum": ""
},
"require": {
"php": ">=5.3.2"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"autoload": {
"psr-0": {
"Valitron": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD"
],
"authors": [
{
"name": "Vance Lucas",
"email": "vance@vancelucas.com",
"homepage": "http://www.vancelucas.com"
}
],
"description": "Simple, elegant, stand-alone validation library with NO dependencies",
"homepage": "http://github.com/vlucas/valitron",
"keywords": [
"valid",
"validation",
"validator"
],
"time": "2015-12-15 20:10:29"
}
],
"packages-dev": [],
......
......@@ -7,6 +7,11 @@ class AppController {
function AppController() {
include_once("vendor/autoload.php");
Valitron\Validator::langDir(__DIR__.'/../vendor/vlucas/valitron/lang'); // always set langDir before lang.
Valitron\Validator::lang('fr');
$this->context = array();
if(!array_key_exists("a", $_REQUEST) || $_REQUEST["a"] == "") {
......@@ -73,10 +78,32 @@ class AppController {
}
function _create($render = "list") {
// behaviour :
// if create is called via API
// SUCCESS => it sends back the created object + 201 Created
// FAILURE => it sends back 422 Unprocessable Entity + error in json
// if create is called via index
// SUCCESS + $render == list => fetch_all + render list
// SUCCESS + $render == edit => fetch(created) + render edit
// FAILURE => 500 + data_exception or whatever
try {
$classname = $this->model;
$object = new $classname(0);
if(!$object->validate($errors)) {
if($this->format == "json") {
header($_SERVER['SERVER_PROTOCOL']." 422 Unprocessable entity", true, 422);
echo json_encode($errors);
exit();
}
}
if($object->create()) {
if($this->format == "json") {
header($_SERVER['SERVER_PROTOCOL']." 201 Created", true, 201);
echo json_encode($object);
exit();
}
$this->set_message("Création effectuée");
} else {
$this->set_message("Aucun nouvel objet n'a été créé");
......
{% extends 'modal.html' %}
{% extends 'modal_api.html' %}
{% block modal_title %}
{% if object.id %} Catégorie ESAR : {{ object.name }} {% else %} Nouvelle catégorie ESAR {% endif %}
{% endblock %}
{% block modal_body %}
<div class="alert alert-info alert-dismissible" role="alert" style="display: none" id="created_ok">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<span class="glyphicon glyphicon-ok"></span>
Création effectuée
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Nom</label>
<div class="col-sm-4">
<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>
<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>
</div>
</div>
{% endblock %}
{% block javascript_fields %}
var fields = ['name', 'label'];
{% endblock %}
{% block javascript_msg %}
var msg = 'Voulez-vous réellement supprimer cette catégorie ?\n' +
'Cette action n\'est possible que si celle-ci n\'a pas été\n' +
......
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">
<span class="glyphicon glyphicon-list-alt" style="margin-right: 10px;" ></span>
{% block modal_title %}{% endblock %}
</h4>
</div>
<div class="modal-body">
{% block modal_body %}{% endblock %}
</div>
<div class="modal-footer">
<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-danger" id="delete_button" value="Supprimer">
{% else %}
<input type="hidden" id="modal_object_id" value="0">
<input type="button" class="btn btn-success" id="save_button" value="Créer">
{% endif %}
</div>
</div>
<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();
});
$.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();
});
return false;
});
return true;
} else {
$('#i').val($('#modal_object_id').val());
$('#a').val('update');
}
document.defaultform.submit();
return true;
});
$('#delete_button').click(function(){
{% block javascript_msg %}{% endblock %}
if(confirm(msg)) {
$('#a').val('delete');
document.defaultform.submit();
}
});
</script>
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