Commit 972f60a2 authored by Vassilis Kanellopoulos's avatar Vassilis Kanellopoulos Committed by kanellov
Browse files

trying to do template with form values and errors

parent cefed6c8
...@@ -48,7 +48,8 @@ return function (Slim\App $app) { ...@@ -48,7 +48,8 @@ return function (Slim\App $app) {
$c->get('view'), $c->get('view'),
$c->get(GrEduLabs\Schools\Service\AssetServiceInterface::class), $c->get(GrEduLabs\Schools\Service\AssetServiceInterface::class),
$c->get(GrEduLabs\ApplicationForm\Service\ApplicationFormServiceInterface::class), $c->get(GrEduLabs\ApplicationForm\Service\ApplicationFormServiceInterface::class),
$c->get(GrEduLabs\ApplicationForm\InputFilter\ApplicationForm::class) $c->get(GrEduLabs\ApplicationForm\InputFilter\ApplicationForm::class),
$c->get('authentication_service')
); );
}; };
}); });
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
ItemRowView = Backbone.View.extend({ ItemRowView = Backbone.View.extend({
tagName: 'tr', tagName: 'tr',
render: function () { render: function (index) {
this.$el.html(this.constructor.template({ index: itemCount })); this.$el.html(this.constructor.template({ index: index | 0}));
itemCount += 1;
return this; return this;
} }
}, { }, {
...@@ -19,15 +19,21 @@ ...@@ -19,15 +19,21 @@
ItemsView = Backbone.View.extend({ ItemsView = Backbone.View.extend({
el: '#items-list', el: '#items-list',
itemCount: 0,
events: { events: {
'click .add-row': 'addRow', 'click .add-row': 'addRow',
'click .remove-row': 'removeRow' 'click .remove-row': 'removeRow'
}, },
initialize: function () { initialize: function () {
this.addRow(); this.itemCount = this.$el.find('tbody tr').length;
if (this.itemCount === 0) {
this.addRow();
}
}, },
addRow: function () { addRow: function () {
this.$el.find('tbody').append(new ItemRowView().render().el); var index = this.itemCount;
this.itemCount += 1;
this.$el.find('tbody').append(new ItemRowView().render(index).el);
return this; return this;
}, },
removeRow: function (evt) { removeRow: function (evt) {
......
...@@ -15,6 +15,7 @@ use GrEduLabs\Schools\Service\AssetServiceInterface; ...@@ -15,6 +15,7 @@ use GrEduLabs\Schools\Service\AssetServiceInterface;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
use Slim\Views\Twig; use Slim\Views\Twig;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\InputFilter\InputFilterInterface; use Zend\InputFilter\InputFilterInterface;
class ApplicationForm class ApplicationForm
...@@ -42,33 +43,51 @@ class ApplicationForm ...@@ -42,33 +43,51 @@ class ApplicationForm
*/ */
protected $appFormInputFilter; protected $appFormInputFilter;
/**
*
* @var AuthenticationServiceInterface
*/
protected $authService;
public function __construct( public function __construct(
Twig $view, Twig $view,
AssetServiceInterface $assetsService, AssetServiceInterface $assetsService,
ApplicationFormServiceInterface $appFormService, ApplicationFormServiceInterface $appFormService,
InputFilterInterface $appFormInputFilter InputFilterInterface $appFormInputFilter,
AuthenticationServiceInterface $authService
) { ) {
$this->view = $view; $this->view = $view;
$this->assetsService = $assetsService; $this->assetsService = $assetsService;
$this->appFormService = $appFormService; $this->appFormService = $appFormService;
$this->appFormInputFilter = $appFormInputFilter; $this->appFormInputFilter = $appFormInputFilter;
$this->authService = $authService;
} }
public function __invoke(Request $req, Response $res) public function __invoke(Request $req, Response $res)
{ {
$school = $req->getAttribute('school'); $school = $req->getAttribute('school');
if ($req->isPost()) { if ($req->isPost()) {
$this->appFormInputFilter->setData(array_merge($req->getParams(), [ $this->appFormInputFilter->setData(array_merge($req->getParams(), [
'school_id' => $school->id, 'school_id' => $school->id,
'submitted_by'=> $this->authService->getIdentity()->mail,
])); ]));
if ($this->appFormInputFilter->isValid()) { $isValid = $this->appFormInputFilter->isValid();
$data = $this->appFormInputFilter->getValues(); if ($isValid) {
$this->appFormService->submit($data); $data = $this->appFormInputFilter->getValues();
$appForm = $this->appFormService->submit($data);
var_dump($appForm);
die();
} }
var_dump($this->appFormInputFilter->getMessages());
die('error'); $this->view['form'] = [
$this->view['messages'] = $this->appFormInputFilter->getMessages(); 'is_valid' => $isValid,
'values' => $this->appFormInputFilter->getValues(),
'raw_values' => $this->appFormInputFilter->getRawValues(),
'messages' => $this->appFormInputFilter->getMessages(),
];
var_dump($this->view['form']);
} }
$res = $this->view->render($res, 'application_form/form.twig', [ $res = $this->view->render($res, 'application_form/form.twig', [
......
...@@ -41,12 +41,10 @@ class ApplicationForm extends InputFilter ...@@ -41,12 +41,10 @@ class ApplicationForm extends InputFilter
$newLabPerspective = new Input('new_lab_perspective'); $newLabPerspective = new Input('new_lab_perspective');
$newLabPerspective->setRequired(true) $newLabPerspective->setRequired(true)
->getFilterChain() ->getValidatorChain()
->attach(new Filter\ToInt());
$newLabPerspective->getValidatorChain()
->attach(new Validator\NotEmpty()) ->attach(new Validator\NotEmpty())
->attach(new Validator\InArray([ ->attach(new Validator\InArray([
'haystack' => [0, 1], 'haystack' => ['ΝΑΙ', 'ΟΧΙ'],
])); ]));
$comments = new Input('comments'); $comments = new Input('comments');
...@@ -55,11 +53,17 @@ class ApplicationForm extends InputFilter ...@@ -55,11 +53,17 @@ class ApplicationForm extends InputFilter
->attach(new Filter\StripTags()) ->attach(new Filter\StripTags())
->attach(new Filter\StringTrim()); ->attach(new Filter\StringTrim());
$submittedBy = new Input('submitted_by');
$submittedBy->setRequired(true)
->getValidatorChain()
->attach(new Validator\NotEmpty())
->attach(new Validator\EmailAddress());
$this->add($schoolId) $this->add($schoolId)
->add($applyFor) ->add($applyFor)
->add($newLabPerspective) ->add($newLabPerspective)
->add($comments) ->add($comments)
->add($itemsInputFilter, 'items') ->add($submittedBy)
; ->add($itemsInputFilter, 'items');
} }
} }
...@@ -20,13 +20,13 @@ class ApplicationFormItem extends InputFilter ...@@ -20,13 +20,13 @@ class ApplicationFormItem extends InputFilter
{ {
public function __construct( public function __construct(
LabServiceInterface $labService, LabServiceInterface $labService,
AssetServiceInterface $assetsService AssetServiceInterface $assetsService
) { ) {
$lab_id = new Input('lab_id'); $labId = new Input('lab_id');
$lab_id->setRequired(true) $labId->setRequired(true)
->getFilterChain() ->getFilterChain()
->attach(new Filter\ToInt()); ->attach(new Filter\ToInt());
$lab_id->getValidatorChain() $labId->getValidatorChain()
->attach(new Validator\NotEmpty()); ->attach(new Validator\NotEmpty());
$itemCategoryId = new Input('itemcategory_id'); $itemCategoryId = new Input('itemcategory_id');
...@@ -66,7 +66,8 @@ class ApplicationFormItem extends InputFilter ...@@ -66,7 +66,8 @@ class ApplicationFormItem extends InputFilter
$reasons->getValidatorChain() $reasons->getValidatorChain()
->attach(new Validator\NotEmpty()); ->attach(new Validator\NotEmpty());
$this->add($itemCategoryId) $this->add($labId)
->add($itemCategoryId)
->add($qty) ->add($qty)
->add($reasons); ->add($reasons);
} }
......
...@@ -32,5 +32,36 @@ class ApplicationFormService implements ApplicationFormServiceInterface ...@@ -32,5 +32,36 @@ class ApplicationFormService implements ApplicationFormServiceInterface
$appForm->apply_for = $data['apply_for']; $appForm->apply_for = $data['apply_for'];
$appForm->new_lab_perspective = $data['new_lab_perspective']; $appForm->new_lab_perspective = $data['new_lab_perspective'];
$appForm->comments = $data['comments']; $appForm->comments = $data['comments'];
$appForm->submitted = time();
$appForm->submitted_by = $data['submitted_by'];
$items = [];
foreach ($data['items'] as $itemData) {
$item = R::dispense('applicationformitem');
// $item->lab_id = $itemData['lab_id'];
$item->itemcategory_id = $itemData['itemcategory_id'];
$item->qty = $itemData['qty'];
$item->reasons = $itemData['reasons'];
$items[] = $item;
}
if (!empty($items)) {
$appForm->ownApplicationformitemList = $items;
}
R::store($appForm);
return $this->exportApplicationForm($appForm);
}
private function exportApplicationForm(\RedBeanPHP\OODBBean $bean)
{
$appForm = $bean->export();
$appForm['items'] = array_map(function ($itemBean) {
return array_merge($itemBean->export(), [
'lab' => $itemBean->lab->name,
'itemcategory' => $itemBean->itemcategory->name,
]);
}, $bean->ownApplicationformitemList);
return $appForm;
} }
} }
{% extends "layout.twig" %} {% extends "layout.twig" %}
{% macro itemSelect(name, label, options, selected) %} {% macro itemSelect(name, label, options, selected, index) %}
<select class="form-control input-sm" name="items[<%= index %>][{{ name }}]"> <select class="form-control input-sm" name="items[{{ index | default('<%= index %>') | raw }}][{{ name }}]">
<option value="" disabled selected>{{ label|striptags }}</option> <option value="" disabled selected>{{ label|striptags }}</option>
{% for option in options %} {% for option in options %}
<option value="{{ option.value|e }}"{% if selected == option.value %} selected{% endif %}> <option value="{{ option.value|e }}"{% if selected == option.value %} selected{% endif %}>
...@@ -11,11 +11,11 @@ ...@@ -11,11 +11,11 @@
</select> </select>
{% endmacro %} {% endmacro %}
{% macro itemCount(name, label, value) %} {% macro itemCount(name, label, value, index) %}
<div class="form-group"> <div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-9"> <div class="col-xs-12 col-sm-12 col-md-9">
<input placeholder="{{ label|striptags }}" class="form-control input-sm" <input placeholder="{{ label|striptags }}" class="form-control input-sm"
type="number" name="items[<%= index %>][{{ name }}]" value="{{ value|default('')|raw }}"> type="number" name="items[{{ index | default('<%= index %>') }}][{{ name }}]" value="{{ value|default('')|raw }}">
</div> </div>
</div> </div>
{% endmacro %} {% endmacro %}
...@@ -34,13 +34,13 @@ ...@@ -34,13 +34,13 @@
</div> </div>
{% endmacro %} {% endmacro %}
{% macro yesno(name, label) %} {% macro yesno(name, label, selected) %}
<div class="form-group"> <div class="form-group">
<label class="control-label hidden-xs hidden-sm" for="el-{{ name }}">{{ label|raw }}</label> <label class="control-label hidden-xs hidden-sm" for="el-{{ name }}">{{ label|raw }}</label>
<select class="form-control" name="{{ name }}" id="el-{{ name }}"> <select class="form-control" name="{{ name }}" id="el-{{ name }}">
<option value="" disabled selected>{{ label|striptags }}</option> <option value="" disabled selected>{{ label|striptags }}</option>
<option value="1">Ναι</option> <option value="ΝΑΙ"{% if selected == 'ΝΑΙ' %} selected{% endif %}>Ναι</option>
<option value="0">Όχι</option> <option value="ΟΧΙ"{% if selected == 'ΟΧΙ' %} selected{% endif %}>Όχι</option>
</select> </select>
</div> </div>
{% endmacro %} {% endmacro %}
...@@ -84,9 +84,9 @@ ...@@ -84,9 +84,9 @@
<tbody> <tbody>
{% for asset in assets %} {% for asset in assets %}
<tr> <tr>
<td>{{ asset.labName }}</td> <td>{{ asset.lab }}</td>
<td>{{ asset.typeName }}</td> <td>{{ asset.type }}</td>
<td>{{ asset.quantity }}</td> <td>{{ asset.qty }}</td>
</tr> </tr>
{% else %} {% else %}
<tr> <tr>
...@@ -117,7 +117,26 @@ ...@@ -117,7 +117,26 @@
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
<tbody></tbody> <tbody>
{% for item in form.values.items %}
<tr>
<td>{{ macros.itemSelect('lab_id', 'Χώρος', lab_choices, item.lab_id, loop.index0 ) }}</td>
<td>{{ macros.itemSelect('itemcategory_id', 'Τύπος', type_choices, item.itemcategory_id, loop.index0) }}</td>
<td>{{ macros.itemCount('qty', 'Πλήθος', item.qty, loop.index0) }}</td>
<td>
<textarea class="form-control input-sm"
rows="1" cols="20"
name="items[{{ loop.index0 }}][reasons]"
placeholder="Αιτιολογία χρήσης">
{{ item.reasons }}
</textarea>
</td>
<td><button type="button" class="btn btn-xs text-danger remove-row">
<i class="fa fa-remove"></i>
</button</td>
</tr>
{% endfor %}
</tbody>
<tfoot> <tfoot>
<tr> <tr>
<td colspan="5" class="text-right"> <td colspan="5" class="text-right">
...@@ -134,9 +153,11 @@ ...@@ -134,9 +153,11 @@
</div> </div>
<fieldset> <fieldset>
<legend>Επιπλέον</legend> <legend>Επιπλέον</legend>
{{ macros.select('apply_for', 'Αίτημα του σχολείου για', apply_for_choices) }} {{ macros.select('apply_for', 'Αίτημα του σχολείου για',
{{ macros.yesno('new_lab_perspective', 'Ύπαρξη - προοπτική αίθουσας για δημιουργία νέου εργαστηρίου')}} apply_for_choices, form.values.apply_for) }}
{{ macros.text('comments', 'Σχόλια/Παρατηρήσεις', '') }} {{ macros.yesno('new_lab_perspective', 'Ύπαρξη - προοπτική αίθουσας για δημιουργία νέου εργαστηρίου',
form.values.new_lab_perspective)}}
{{ macros.text('comments', 'Σχόλια/Παρατηρήσεις', form.value.comments) }}
</fieldset> </fieldset>
<hr> <hr>
...@@ -150,7 +171,9 @@ ...@@ -150,7 +171,9 @@
<td>{{ macros.itemSelect('lab_id', 'Χώρος', lab_choices) }}</td> <td>{{ macros.itemSelect('lab_id', 'Χώρος', lab_choices) }}</td>
<td>{{ macros.itemSelect('itemcategory_id', 'Τύπος', type_choices) }}</td> <td>{{ macros.itemSelect('itemcategory_id', 'Τύπος', type_choices) }}</td>
<td>{{ macros.itemCount('qty', 'Πλήθος') }}</td> <td>{{ macros.itemCount('qty', 'Πλήθος') }}</td>
<td><textarea class="form-control input-sm" rows="1" cols="20" name="items[<%= index %>][reasons]" placeholder="Αιτιολογία χρήσης"></textarea></td> <td><textarea class="form-control input-sm" rows="1" cols="20"
name="items[<%= index %>][reasons]"
placeholder="Αιτιολογία χρήσης"></textarea></td>
<td><button type="button" class="btn btn-xs text-danger remove-row"> <td><button type="button" class="btn btn-xs text-danger remove-row">
<i class="fa fa-remove"></i> <i class="fa fa-remove"></i>
</button</td> </button</td>
......
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