Skip to content

Commit

Permalink
Merge pull request #191 from atk4/feature/validation-dimmer
Browse files Browse the repository at this point in the history
Implement nice validation error display
  • Loading branch information
romaninsh authored Jul 9, 2017
2 parents 732c12b + 43ccfb9 commit 23819e9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
5 changes: 5 additions & 0 deletions demos/button2.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@
$b->on('click', function ($b) {
return 'success';
});

$b = $layout->add(new Button('failure'));
$b->on('click', function ($b) {
throw new \atk4\data\ValidationException(['Everything is bad']);
});
7 changes: 3 additions & 4 deletions demos/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

require '../vendor/autoload.php';

$app = new \atk4\ui\App([
'Agile UI v1.1 - Demo Suite',
'icon'=> 'user',
]);
$app = new \atk4\ui\App();

$app->title = 'Agile UI - Demo Suite';

if (file_exists('../public/atk4JS.min.js')) {
$app->cdn['atk'] = '../public';
Expand Down
74 changes: 45 additions & 29 deletions src/jsCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,60 @@ public function set($callback, $args = [])
}

return parent::set(function () use ($callback) {
$chain = new jQuery(new jsExpression('this'));
try {
$chain = new jQuery(new jsExpression('this'));

$values = [];
foreach ($this->args as $key=>$value) {
$values[] = isset($_POST[$key]) ? $_POST[$key] : null;
}
$values = [];
foreach ($this->args as $key=>$value) {
$values[] = isset($_POST[$key]) ? $_POST[$key] : null;
}

$response = call_user_func_array($callback, array_merge([$chain], $values));
$response = call_user_func_array($callback, array_merge([$chain], $values));

if ($response === $chain) {
$response = null;
}
if ($response === $chain) {
$response = null;
}

$actions = [];
$actions = [];

if ($chain->_chain) {
$actions[] = $chain;
}
if ($chain->_chain) {
$actions[] = $chain;
}

$response = $this->flatternArray($response);

foreach ($response as $r) {
if (is_string($r)) {
$actions[] = new jsExpression('alert([])', [$r]);
} elseif ($r instanceof jsExpressionable) {
$actions[] = $r;
} elseif ($r === null) {
continue;
} else {
throw new Exception(['Incorrect callback. Must be string or action.', 'r'=>$r]);
$response = $this->flatternArray($response);

foreach ($response as $r) {
if (is_string($r)) {
$actions[] = new jsExpression('alert([])', [$r]);
} elseif ($r instanceof jsExpressionable) {
$actions[] = $r;
} elseif ($r === null) {
continue;
} else {
throw new Exception(['Incorrect callback. Must be string or action.', 'r'=>$r]);
}
}
}

$ajaxec = implode(";\n", array_map(function (jsExpressionable $r) {
return $r->jsRender();
}, $actions));
$ajaxec = implode(";\n", array_map(function (jsExpressionable $r) {
return $r->jsRender();
}, $actions));

$this->app->terminate(json_encode(['success'=>true, 'message'=>'Success', 'eval'=>$ajaxec]));
} catch (\atk4\data\ValidationException $e) {
// Validation exceptions will be presented to user in a friendly way

$actions = [];
$actions[] = new jsExpression('alert([])', [$e->getMessage()]);

$this->app->terminate(json_encode(['success'=>true, 'message'=>'Success', 'eval'=>$ajaxec]));
$ajaxec = implode(";\n", array_map(function (jsExpressionable $r) {
return $r->jsRender();
}, $actions));

$m = new Message($e->getMessage());
$m->addClass('error');

$this->app->terminate(json_encode(['success'=>false, 'message'=>$m->getHTML()]));
}
});
}
}

0 comments on commit 23819e9

Please sign in to comment.