Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add help_block doesn´t work #238

Closed
LKaemmerling opened this issue Jun 23, 2016 · 17 comments
Closed

Add help_block doesn´t work #238

LKaemmerling opened this issue Jun 23, 2016 · 17 comments

Comments

@LKaemmerling
Copy link

LKaemmerling commented Jun 23, 2016

Hello,

i have the problem that i can't add the help_block (dynamic).

The Code:

<?php
//In buildForm():
$customOptions = [];
$withHelpBlock = true;
// Add Some Other Options (these Options work)
if($withHelpBlock)
$customOptions = array_merge($customOptions, ['help_block' => ['text' => 'Please select your Company','tag' => 'p','attr' => ['class' => 'help-block']]]);
}
$this->add('company','select',$customOptions);

And the result is an Select Input with all other options that i define, but without the helpblock.

So it is the same as in the documentation... Can you help? Or is that a Bug?
Laravel 5.2 and laravel-form-builder 1.7.10
Thank You! @kristijanhusak

@kristijanhusak
Copy link
Owner

Hi, i'll check it out, thanks for reporting.

What version are you using?

@LKaemmerling
Copy link
Author

Laravel 5.2 and laravel-form-builder 1.7.10

@kristijanhusak
Copy link
Owner

It works fine for me. Did you use any older versions, and than update it?
Do you have some custom views?

@LKaemmerling
Copy link
Author

No, complete new development with out custom views. i just use the normal types that are defined out of the box.

The only thing is that it is an form builder for normal employees without knowledge about php or html. I first used.

I have one Formobject that will be dynamicly filled @ buildForm but this can't be a problem, or? i just run add with text, select, checkbox, or so in one foreach. I`ll add the complette buildForm hear.

@LKaemmerling
Copy link
Author

  */
    public function buildForm()
    {

        foreach ($this->_form->inputs as $input){
            /**
             * @var $input Input
             */
            $inputType = InputTypRepository::getRepository()->find($input->type);
            $input_name = ($inputType->inputName === false) ? (($input->object_field == '') ? str_slug($input->label,'') : $input->object_field): $inputType->inputName;
            $readOnly = ($input->isReadOnly()) ? ['disabled'] : [];
            $options = [
                'label' => _($input->label),
                'attr' => (empty($this->formOptions['attr']) ? $readOnly: array_merge($readOnly,$this->formOptions['attr'])),
            ];
            if(!$input->isReadOnly()){
                self::addRule($input_name,($input->isRequired() ? 'required|': '').$inputType->validation_rule);
            }
            if($inputType->formBuilderTyp == 'select'){
                if($inputType->getOptions() !== null){
                    $options = array_merge($options,['choices' => $inputType->getOptions()]);
                } else {
                    $options = array_merge($options,['choices' => explode('|',$input->options)]);
                }
                if($this->model instanceof Model){
                    $options = array_merge($options,['selected' => eval('return $this->model->'.$input->object_field.';')]);
                }
                $options = array_merge($options,['empty_value' => '--- Bitte wählen ---']);
            }

            if($input->helpBlock != null){
                $options['help_block'] = ['text' => $input->helpBlock,'tag' => 'p','attr' => ['class' => 'help-block']];
            }
            $this->add($input_name,$inputType->formBuilderTyp,$options);
        }
        $this->addDefaultActions();

    }

@kristijanhusak
Copy link
Owner

It shouldn't be a problem, but try creating a field without all this code. So just comment it out and try something like this:

$this->add('name', 'text', [
    'help_block' => [
        'text' => 'This is help block message',
        'tag' => 'p',
        'attr' => ['class' => 'help-block']
    ]
);

And if that works, than it's problem in your logic somewhere, if not, it's in the package.

@LKaemmerling
Copy link
Author

Doesn't work too.
image

    public function buildForm()
    {

       /* foreach ($this->_form->inputs as $input){
            /**
             * @var $input Input
             */
          /*  $inputType = InputTypRepository::getRepository()->find($input->type);
            $input_name = ($inputType->inputName === false) ? (($input->object_field == '') ? str_slug($input->label,'') : $input->object_field): $inputType->inputName;
            $readOnly = ($input->isReadOnly()) ? ['disabled'] : [];
            $options = [
                'label' => _($input->label),
                'attr' => (empty($this->formOptions['attr']) ? $readOnly: array_merge($readOnly,$this->formOptions['attr'])),
            ];
            if(!$input->isReadOnly()){
                self::addRule($input_name,($input->isRequired() ? 'required|': '').$inputType->validation_rule);
            }
            if($inputType->formBuilderTyp == 'select'){
                if($inputType->getOptions() !== null){
                    $options = array_merge($options,['choices' => $inputType->getOptions()]);
                } else {
                    $options = array_merge($options,['choices' => explode('|',$input->options)]);
                }
                if($this->model instanceof Model){
                    $options = array_merge($options,['selected' => eval('return $this->model->'.$input->object_field.';')]);
                }
                $options = array_merge($options,['empty_value' => '--- Bitte wählen ---']);
            }

            if($input->helpBlock != null){
                $options['help_block'] = ['text' => $input->helpBlock,'tag' => 'p','attr' => ['class' => 'help-block']];
            }
            $this->add($input_name,$inputType->formBuilderTyp,$options);
        } */
        $this->add('teeest', 'text', [
                'help_block' => [
                    'text' => 'This is help block message',
                    'tag' => 'p',
                    'attr' => ['class' => 'help-block']
                ]
            ]
);
        $this->addDefaultActions();

    }

@kristijanhusak
Copy link
Owner

really strange. What's $this->addDefaultActions() ?

@LKaemmerling
Copy link
Author

Adds only the button:

    /**
     *
     */
    public function addDefaultActions()
    {
              $this->add('submit', 'submit',
            [
                'label' => 'Speichern',
                'attr' => [
                    'class' => 'btn btn-success'
                ],
            ])
            ->add('back', 'button',
                [
                    'label' => 'Zurück',
                    'attr' => [
                        'class' => 'btn btn-default',
                        'onclick' => 'window.history.back()'
                    ],
                ]);

@kristijanhusak
Copy link
Owner

I'm not able to reproduce. What did you put in the view?

@LKaemmerling
Copy link
Author

i just make form($theFormBuilderInstance).

 /**
     * @param string $key
     * @return string
     * @throws \ErrorException
     */
    public function render($key, array $options = [], array $data = [])
    {
        $form = Form::where('key', '=', $key)->first();
        if ($form instanceof Form) {
            if($form->action == 'database'){
                $options = array_merge($options, ['model' => eval('return ' . $form->action_url . ';')]);
            }
            if ($form->action == 'url') {
                $options = array_merge($options,['url' => $form->action_url]);
            }
            $form = $this->_container
                ->make(DynamicForm::class, [$form])
                ->setFormHelper($this->_formHelper)
                ->setFormBuilder($this)
                ->setFormOptions($options)
                ->addData($data)
                ->setRequest(request());
            $form->buildForm();
            return form($form);
        } else {
            throw new \ErrorException('Can´t find Form with Key: ' . $key);
        }
    }

Form:: is as Model that represents the form in the Database in the Form i only make

 {!! FormRender::render('key_from_form') !!}

@kristijanhusak
Copy link
Owner

can you just create a form with FormBuilder class in a controller, pass it to view, and in view use form($form) ? Similar to this example http://kristijanhusak.github.io/laravel-form-builder/overview/quick-start.html. Just to test it out.

@LKaemmerling
Copy link
Author

I used the complete Sample:

Form:

namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class SongForm extends Form
{
    public function buildForm()
    {
        $this
            ->add('name', 'text')
            ->add('lyrics', 'textarea')
            ->add('publish', 'checkbox',['help_block' => ['text' => 'helpBlock Works!']]);
    }
}

Controller:

    public function test(FormBuilder $formBuilder)
    {
        $form = $formBuilder->create('App\Forms\SongForm', [
            'method' => 'POST',
            'url' => '/test'
        ]);

        return view('welcome', compact('form'));
    }

}

View:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Welcome</div>

                <div class="panel-body">
                    {!! form($form) !!}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@LKaemmerling
Copy link
Author

Result:
image

@LKaemmerling
Copy link
Author

And when i try to modify the publish input:

use Kris\LaravelFormBuilder\Form;

class SongForm extends Form
{
    public function buildForm()
    {
        $this
            ->add('name', 'text')
            ->add('lyrics', 'textarea')
            ->add('publish', 'checkbox');
        $this->modify('publish','checkbox',['help_block' => ['text' => 'helpBlock Works!']],false);
    }
}

I get the Error:
View [] not found.
image

@LKaemmerling
Copy link
Author

@kristijanhusak i think i founded it... first i had https://github.com/Distilleries/FormBuilder installed, with his views. I removed this without thinking about the published views and installed the base builder (your package). This was the error with all ;) Thank you!

@kristijanhusak
Copy link
Owner

Great. Closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants