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

trying to do template with form values and errors

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