-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax_forms.php
39 lines (33 loc) · 1.38 KB
/
ajax_forms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
class AjaxFormsComponent extends Object{
public $components = array('RequestHandler');
public $doBeforeRender = true;
public $viewToRender = false;
public function startup($controller){
$this->controller = $controller;
}
public function beforeRender(){
if ($this->controller->data && $this->RequestHandler->isAjax() && $this->doBeforeRender){
$invalid = array();
$ok = true;
$rendered = null;
foreach($this->controller->data as $model=>$vals){
$this->controller->loadModel($model);
$invalid[$model] = $this->controller->{$model}->validationErrors;
if ($invalid[$model]) $ok = false;
}
//Render the view and save it if needed
if ($this->viewToRender){
$this->doBeforeRender = false;
$this->controller->autoLayout = false;
$rendered = $this->controller->render($this->viewToRender);
}
$data = $this->controller->data;
$object = compact('ok', 'invalid', 'data', 'rendered');
App::import('Helper', 'Javascript');
$js = new JavascriptHelper();
echo $js->object($object);
exit;
}
}
}