Completed
Push — master ( 9c55df...f1d822 )
by tac
02:37
created

EndpointController::anyIndex()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 35
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 17
nc 2
nop 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