kristijanhusak / laravel-form-builder

Laravel Form builder for version 5+!
https://packagist.org/packages/kris/laravel-form-builder
MIT License
1.69k stars 295 forks source link

How to save form values? #380

Open muazzamazaz opened 7 years ago

muazzamazaz commented 7 years ago
   public function store(FormBuilder $formBuilder)
    {
        $form = $formBuilder->create(\App\Forms\JobPosting::class);

       if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
else
$JobTitle = $form->JobTitle;
$JobDetail = $form->Jobdetail;

    DB::table('jobpost')->insert(
     array(
            'userid' => Auth::id(),
            'jobtitle'   =>  $JobTitle ,
            'detail'  =>$JobDetail
     )
);

But I'm getting following error:

Object of class Kris\LaravelFormBuilder\Fields\InputType could not be converted to string

kristijanhusak commented 7 years ago

you can get all data from form with $form->getFieldValues(), and then pull data from that.

muazzamazaz commented 7 years ago

$data = $form->getFieldValues();

show null values

kristijanhusak commented 7 years ago

Did you set up the form in the view template?

muazzamazaz commented 7 years ago

yes there is view code as:


@extends('layouts.app')

@section('content')
    {!! form($form) !!}
    {!! Form::token() !!}
@endsection
kristijanhusak commented 7 years ago

You don't need Form::token(). Did you set up csrf middleware?

tpfuhl commented 6 years ago
    public function store(FormBuilder $formBuilder) {
        $form = $formBuilder->create(\App\Forms\MembersForm::class);
        echo print_r($form->getFieldValues(), true);
        return;
}

$form->getFieldValues() throws an error: Call to undefined method App\Forms\MembersForm::getFieldValues()

kristijanhusak commented 6 years ago

@tpfuhl did you properly extend the Form class from the package?

tpfuhl commented 6 years ago

I did a composer update as outlined in the README. All is working well, except the store function. Perhaps I should pass a Request parameter to the function store(FormBuilder $formBuilder), additionnally to the FormBuilder ? Getting the names is working well, but the Values are empty:

       foreach ($form->getFields() as $field) {
            echo "<br>field name: " . $field->getName();
            echo " ----field value: " . $field->getValue();
        }
tpfuhl commented 6 years ago

I can access the request's raw data (yes, it's ugly) and retrieve the entered form values. It would be nice to get them via a FormBuilder method, though.

$arequest = (array)$form->getRequest();
echo "<pre>---Form Data: ";
$myData = (array)$arequest["request"];
foreach ($myData as $row)
            foreach ($row as $key=>$val)
                echo "\n $key=>$val";
 echo "</pre>";
kristijanhusak commented 6 years ago

@tpfuhl it's really strange that you can call getRequest(), but not call getFieldvalues(). Both methods exist on the form class.

tpfuhl commented 6 years ago

might be a conflict with other packages ? I load these ones from composer.json :

    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "barryvdh/laravel-debugbar": "^2.0@dev",
        "laravelcollective/html": "5.1.*",
        "codingo-me/dropzoner": "^1.0",
        "intervention/image": "^2.3",
        "yajra/laravel-datatables-oracle": "6.1.*",
        "cviebrock/eloquent-sluggable": "3.1.4",
        "doctrine/dbal": "^2.5",
        "twbs/bootstrap": "3.3.7",
        "kris/laravel-form-builder": "1.6.*"
      },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1",
        "ignasbernotas/laravel-model-generator": "^1.2"
    },
tpfuhl commented 6 years ago

I got problems when calling the update function:

public function edit(int $id, FormBuilder $formBuilder) {
        $record = Member::find($id);
        $form = $formBuilder->create(\App\Forms\MembersForm::class, [
            'method' => 'PUT',
            'url' => action('MembersController@update'),
            'model' => $record,
        ]);

The Formbuilder has generated a form without instantiating the {id}:, nonetheless the record is loaded and the values correctly displayed in the form. Probably a silly mistake . Any hints ? <form method="POST" action="http://localhost:8000/members/%7Bid%7D" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT"><input name="_token" type="hidden" value="..."> ...

The defined route is the default route generated by Route::resource('members', 'MembersController'); and I added the routes Route::post( 'members/{id}/update',['middleware' => 'auth', 'uses' => 'MembersController@update']); Route::get( 'members/{id}/update',['middleware' => 'auth', 'uses' => 'MembersController@update']);

Thanx, Th.

kristijanhusak commented 6 years ago

@tpfuhl where do you expect id to exist in the form?