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

choices type field not populating value #29

Closed
sitapraj opened this issue Dec 15, 2014 · 14 comments
Closed

choices type field not populating value #29

sitapraj opened this issue Dec 15, 2014 · 14 comments

Comments

@sitapraj
Copy link

In $this->add('fields', 'choice', [
'choices' => ['choice1' => 'choice1', 'choice2' => 'choice2'],
'empty_value' => 'Select',
'multiple' => TRUE,
'attr' => ['class' => 'multiple-input'],
])
value is not populating through model,passing value as $model->fields = ['choice1']
$form = \FormBuilder::create('Form', [
'method' => 'PUT',
'url' => route('update', $id),
'model' => $model
]);

@kristijanhusak
Copy link
Owner

When you pass the model, you have to manually assign choices:

<?php
    $this->add('fields', 'choice', [
        'choices' => $this->model->fields,
        ...
    ]);

@sitapraj
Copy link
Author

No,No,,,, how to populate the values that are saved for editing

@kristijanhusak
Copy link
Owner

I don't understand your question. When the form is submitted, you should receive selected data in Input::get('fields'), and you handle them after.

@sitapraj
Copy link
Author

ya.. I got that but in edit form how to populate selected values in 'choice' type using model.Means,if I want to pass default value as $model->id = 1,it displays in the form but $model->fields = ['choice1'], not displaying in the form,not showing selected choice,choice1

@kristijanhusak
Copy link
Owner

I'm not 100% sure, but i think that Laravel's form builder does not support automatically checking multiple values. You can try to put like this:

<?php
    $this->add('fields', 'choice', [
        'choices' => $this->model->getAllPossibleFields(),
       'selected' => $this->model->fields
    ]);

selected property holds the keys of all selected values, so if you have ['choice1 => 'some choice', 'choice2' => 'some other choice'], if you want them to both be selected you put:

<?php
'selected' => ['choice1', 'choice2']

@sitapraj
Copy link
Author

Thank You,,, what I need was this 'selected' => $this->model->fields

@kristijanhusak
Copy link
Owner

Great. It should be automatic, but i think that works only when there is one selection.

@sitapraj
Copy link
Author

it works for multiple selection too...

@sitapraj
Copy link
Author

But, how this 'selected' => $this->model->fields can be use for subforms,,Subforms generate field name as name="subFormClassName[field]"????

@kristijanhusak
Copy link
Owner

Same way, when you initialize it, pass model and use it.

@sitapraj
Copy link
Author

if sub form attributes are the attributes of a model then how value can be populated. subFormClassName[field] is the attribute of model. $model->subFormClassName[field] = "value".

@kristijanhusak
Copy link
Owner

I think just passing the model to the subform should work.

<?php
$this->add('sub_form', 'form', [
    'class' => \FormBuilder::create('Forms/SubForm', ['model' => $this->model])
    // ...
]);

@sitapraj
Copy link
Author

but passing value like this $model->subFormClassName[field] = "value" is not populating in the form

@kristijanhusak
Copy link
Owner

yes, it's not working that way. It's not supported that way by laravel form builder class.

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