I have the following code in my view:
{!! Form::select( 'projectSkills',
['' => ''] + \App\Skill::where('type','freelancer')
->pluck('name', 'id')
->toArray(),
#$skills,
[
'class' => 'select2',
'multiple'=>'multiple'
]
) !!}
Where the value of the options comes as 0,1,2,3,4,5,
I want the value as a name which is fetching from the table.
What am i doing wrong there ?
You are making a lists consisting of: [id => name] by calling pluck('name','id')
This will be fed into the form causing a dropdown list:
<option value="id">name</option>
If you wish to change the 'value' part, you need to supply an array consisting of the correct value part.
You could do pluck('name','name'); to get the values you want into the form.
If you look at the the documentation of the method pluck you see that the second parameter is for the 'key' part of the array that will be returned.
Related
I am using Yii2 with ActiveFrom. I have form of user which add multiple names, email etc.
For example:
echo $form->field($model, "name[$i]")->widget(kartik\select2\Select2::classname(), [
'data' => $name_master,
'options' => ['placeholder' => 'Select drop down', 'multiple' => false, 'class' => 'selectpicker form-control'],
])->label(false);
I am able to save the value at the time of adding it. But when I try to open it in edit form and assign value to model key I am getting error.
Assign value to key for edit mode.
foreach ($namesDump as $val) {
$objectKey = "name";
$model->$objectKey[$index] = $val['name'];
}
It is give me error can someone tell me how we can assign value to object key which is array form.
You probably should use {} to indicate precedence for property name - it is different depending on PHP version:
$model->{$objectKey}[$index] = $val['name'];
I need to populate some entity value into the radio HTML code, I look
through all documentation but does not seem to be able to find any help
below is the php code, {{entity->description}} is the entity value I want to show
$this->Form->setTemplates ( [
'radioContainer' => '{{content}}'
] );
echo $this->Form->control ( 'OrderShippingMethodId',
[
'templates' => [
'nestingLabel' => '**<p>{{entity->description}}</p>**
{{input}}{{text}}',
'radioWrapper' => '{{label}}' ],
'options' => $shippingMethodsRadio,
'type' => 'radio',
'label' => false,
'type' => 'radio',
'id' => 'OrderShippingMethodId',
'legend' => false ,
'escape' => false
] );
it generate the below html code, the <p></p> is not showing the entity description value
Shipping
as can be seen in the bold code above, the entity->description value cannot be read
out, seem that the only value the form->control can read out from the
entity is text, value. is there anyway to access other value in the entity
in the controller method, to populate $shippingMethodsRadio
$this->set('shippingMethodsRadio' => $shippingMethods->extract('radio'));
the $shippingMethods, will contain a collection of the entity shippingMethod, I want to read other value like description property in the entity into the radio control, the radio property in the shippingMethod return an array contains value, text, description property of the entity to populate the radio control
thank you
I'm trying to define some ChoiceType in Symfony 3.0.9 in forms where there are populated with ajax depending on how many options are created dynamical, but then the validation form says that the option selected is not valid.
I define the ChoiceType like this:
->add('position', ChoiceType::class, [
'placeholder' => 'Select position',
'choices' => [],
'attr' => [
'class' => 'form-control choice-position'
],
])
And the error I get is:
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[lessonGroups].children[0].children[position] = 1
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "position": The choice "1" does not exist or is not unique
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
The choice "1" does not exist or is not unique
I don't know if there's needed any further information.
Thank you!
Because your definition does not include any choices ('choices' => [],) Symfony detect than a user try to submit a result not in the initial authorized results.
You could set an initial array of choices containing all the values available or you could disable that validation by using:
$builder->get('yourField')->resetViewTransformers();
I want to pass get all existing locales to view. This is my code
view
{!! Form::select('language', $languages,null, ['placeholder' => 'Pick a language']) !!}
controller
this only pull the current how can I pull all with eloquent
$languageCurrent = App::getLocale();
How can I pass it into view(when I'm manipulating data from database I can return with something like this)
->with('users', $users)
How can I return value as array
If you have multiple locales defined in config/app.php, like described here:
'locales' => ['en' => 'English', 'sv' => 'Swedish'],
You could try to do this:
{!! Form::select('language', array_flip(config('app.locales')), null, ['placeholder' => 'Pick a language']) !!}
config() will get locales list and array_flip() will swap keys and values for Form::select.
You can add an array in /config/app.php containing the locales which you use, for example : 'locales' => ['en' => 'English', 'pl' => 'Polish'] than you should be able to use config() helper function to get the values like $available_locales=config('app.locales');
I have a form containing text inputs and select box. I was trying to apply laravel validation. Now i wanted to retain the user inputted values, if validation doesn't success.
I am able to get this done on input box, but not on select box. How to show previously selected value(in select box) if validation doesn't pass.
This is my code
{{Form::select('vehicles_year', $modelYears, '-1', ['id' => 'vehicles_year'])}}
<span class="help-block" id="vehicles_year_error">
#if ($errors->has('vehicles_year')) {{$errors->first('vehicles_year')}} #endif
</span>
-1 is the key of default value that i am showing when the form loads.
What I do is that I add "default" option which value is set to "nothing".
In validation rules I say this value is required. If user does not pick
one of the other options, validation fails.
$options = [
'value' => 'label',
'value' => 'label',
'value' => 'label',
'value' => 'label',
];
$options = array_merge(['' => 'please select'], $options);
{{ Form::select('vehicles_year', $options, Input::old('vehicles_year'), ['id' => 'vehicles_year']) }}
#if ($errors->has('vehicles_year'))
<span class="help-block" id="vehicles_year_error">
{{ $errors->first('vehicles_year') }}
</span>
#endif
// Validation rules somewhere...
$rules = [
...
'vehicles_year' => 'required|...',
...
];
The controller code is missing. I will suppose your handle the POST in a controller method and then you Redirect::back()->withInput();
Thanks to ->withInput() You can use in your view something like Input::old() to get the Input values of your previous request.
So to select your previous item AND have -1 by default, you can use Input::old('vehicles_year', -1)
The first argument is the Input name, and the second is the default value.
{{Form::select('vehicles_year', $modelYears, Input::old('vehicles_year', -1), ['id' => 'vehicles_year'])}}
Hope this helps