Completed
Branch master (f1d822)
by tac
03:30 queued 01:02
created

EndpointController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 39
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Tacone\Bees\Demo\Controllers;
4
5
use Tacone\Bees\Demo\Models\Article;
6
use Tacone\Bees\Widget\Endpoint;
7
use View;
8
9
class EndpointController extends DemoController
10
{
11
    public function anyIndex()
12
    {
13
        // load the data
14
        $model = Article::findOrNew(1);
15
16
        // instantiate the form
17
        $form = new Endpoint($model);
18
19
        // define the fields
20
        $form->text('title')->rules('required|max:10');
21
        $form->text('author.firstname', 'Author\'s first name')
22
            ->rules('required');
23
        $form->text('author.lastname');
24
        $form->textarea('detail.note');
25
        $form->textarea('body');
26
        $form->select('public')->options([
27
            0 => 'No',
28
            1 => 'Yes',
29
        ]);
30
31
        // read the POST data, if any
32
        $form->populate();
33
34
        // write new data back to the model
35
        $form->writeSource();
36
37
        // if the form has been sent, and has no errors
38
        if ($form->submitted() && $form->validate()) {
39
            // we will save the data in the database
40
            $form->save();
41
        }
42
43
        // we just need to pass the $form instance to the view
44
        return $form->toArray();
45
    }
46
47
}
48