I have this form.
{!! Form::open(['action' => 'ArticlesController#store', 'method' => 'post', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
Form::select('size', array(
'L' => 'Large',
'S' => 'Small'
));
</div>
{!! Form::close() !!}
The user will have a dropdown list to select Large (value: L) or Small (value: S). But if the user, let say, changes the value of any of those options using the dev tools, or whatever.
How can I validate the form if the user sends the 'size' field with a value that wasn't originally in the select options?
I mean, how can I check that the sent value is L or S, but not anything else.
Because the user could easily edit the form and send whatever value he wants to send, he could send a value that wasn't suppose to be sent.
I can do that using the validate class, but if instead of a 2 options list it is a 100 options list that'd be impossible.
Thanks!
This is how you can validate that:
$request->validate(['size' => 'required|in:L,S']);
this part after pipe "|in:L,S'" is used to check if the $request attribute value is equal to any value in that rule.
https://laravel.com/docs/5.5/validation#rule-in
Related
So I have this code to display one radio button option in a ChoiceType field.
{{ form_widget(form.persontype[0], {"attr":{"id":"student"}}) }}
based on reading the net (mostly discussions here in stackoverflow), it should theoretically change the ID of the radio button. BUT it's not working. Any ideas on how to get it work?
This is my Form builder code in relation to the ChoiceType field.
->add(
"persontype"
, "choice"
, array(
"choices" => array("student"=>"Student", "staff"=>"Staff")
, "expanded" => true
, "label_attr" => array(
"class" => "normal"
)
, "multiple" => false
, "required" => false
)
)
Thanks for the help.
I'm pretty sure its not meant to overwrite in an easy way as ichabrand already wrote before :)
Not sure if this is the answer but i've seen it another day:
{{ form_widget(form.persontype[0], {"id":"student"}) }}
This id is made with your formulaire name and your field name : "formulaire[field]", i'm not sure than you can overpass that.
If you want to add custom field, you have to add 'allow_extra_fields' => true in your form options.
If you use default form_div_layout as your form components template, you can notice that attributes for each of your inputs generated using the next block:
{%- block widget_attributes -%}
id="{{ id }}" name="{{ full_name }}"
...
So to change id, you have 2 options:
rewrite widget_attributes by using custom form theme
overwrite buildView or finishView of your form type and after call of parent::buildView just add some code to modify vars['id'] of needed control
I have a form that has a select input that called customer_id. This field is not required, and I fetch the options via the Customer Model. My code looks like this:
{!! Form::select('customer', $customers->lists('email', 'id'), null, ['class' => 'form-control']) !!}
so far so good, right? but the thing is that when I don't want to choose a customer, I can't choose a blank option. Is there a way to add a blank option so I wouldn't need to pick a customer all the time?
[''=>'---']+$customers->lists('email', 'id')->toArray()
or
array_merge([''=>'---'], $customers->lists('email', 'id')->toArray())
Is there some way to insert a blank option when creating a select input with the Laravel FormBuilder? I have this at the moment but I want to change it
{!! Form::select('tipo_id', ['blank' => ''] + $tipos, null, ['class' => 'form-control']) !!}
You cannot do too much better than you are already doing. Secret is to have array first element empty to show blank input.
I have 2 email fields and I am using
{{ Form::text('email[]', Input::old('email'),array('class' => 'large-2', 'placeholder' => 'email address','id'=>'email')) }}`
if I use [] to get multiple values for same variable, it is giving error in view page if posted back some data. for example if some fields are mandatory and if user fail to fill those, page will be redirected to same view page from where it was launched.
In such cases it is showing error.
How to fix this issue?
One text field can contain only one data. So, you need another text field to achieve that. if you don't want to show the multiple emails, you can use hidden fields.
{{ Form::hidden('email[]', Input::old('email1'))
{{ Form::hidden('email[]', Input::old('email2'))
Or you can use select. Laravel allow array data if use select field.
For example:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small')) }}
http://laravel.com/docs/html#drop-down-lists
My app contains a form with three choice fields / dropdown lists.
The first is linked to a "relational" field in the entity, and works fine.
The user is supposed to choose a table link first.
The second and third are both linked to string fields in that same entity.
Through jQuery, these choice fields get populated with values based on the decision the user makes in the first dropdown list.
After submitting the form, there is an error for the second and third field: they contain invalid input. When I check the form object, their values were not bound; their values never arrived.
The choice fields for collecting string data from the user looks like this:
->add('sourceName', 'choice', array
(
'label' => 'Choose source name:',
'empty_value' => 'Please choose a table link first...',
'choices' => array(),
'attr' => array('class' => 'extFieldChoice'),
)
After jQuery has done its job, the html select element looks like this:
<select id="someId" name="someName[sourceName]" required="required"
class="extFieldChoice">
<option value="first">first</option>
<option value="second">second</option>
<option value="manymore">Many more...</option>
</select>
I suspect that the error can be found in the initially empty choices array. However, it would be impossible to fill it with all possible choices, because they run in the hundreds.
I've the same problem few days ago and it drive me nuts to find a solution, I get to this question and even probably it's too late I want to share the solutions I've found in case there'll be someone with the same problem, I have found three of them, but none of them seems to be the perfect solution.
In my case I have to save the city selected by an user based on the zipCode. When a user save a new address he wrotes the zipCode and then via ajax I fill-in the city select with options of the cities.
My "choice" field declaration:
$builder->add('city', 'choice', array(
'label' => 'form.city',
'read_only' => true,
'mapped' => false,
'required' => false,
'empty_value' => false,
'choices' => array('none' => 'form.empty.city'),
'data' => null
));
The problem is that the form validation look for two things:
If the form it's related with an entity looks forward the entity
to validate, you can skip this validation easily with the "mapped"
=> false parameter.
The form validation itself, if you have a "choice" type field with or without choices defined when the form validate look towards the first choices you have declared. And I haven't been able to skip the validation for only this field.
So the three ways I have found:
Use the form event and before whe bind the request to the form $builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent $event) use ($xxx) { ...}); (I have an example of use at the linked article) we make the same query to the database we have made on the ajax, we retrieve the values and add them to the chocie field. This is the better way if we think in code, but we have to do the same query twice, and I don't want to do that.
In my case I have to store a string with the city name so another option is to add the city field as a hidden one and insert the whole select as an element. But I don't like this one for many reasons, two of them: I don't like the idea of insert a hole , with and between the other form fields created by symfony2; the other one is that it requires more jquery that it's necessary from my point of view.
Based on the second option, I was filling in some other hidden fields based on the city selection the user has made, so I only include one more hidden field to save the city name; and when the form is submited I remove all the options from the select, so it matches the choices I have defined.
$('#cityChoice').on({
change: function(){
var optionSelected = $(this).find('option:selected');
$('#city').val(optionSelected.val());
$('#latitude').val(optionSelected.data('lat'));
$('#longitude').val(optionSelected.data('lng'));
}
});
$('#myForm').on({
submit: function(){
$('#mySelect option').remove();
}
});
I've decided for the third option, but I think the three of them have bad sides.