Laravel ignores select input validation if no option is selected - php

I have a form with some fields which I want to validate using Laravel's validate() method.
public function postSomething(Request $req) {
...
$this->validate($req, [
'text_input' => 'required',
'select_input' => 'required'
]);
...
}
The issue is that if the form is submitted without selecting an option from the select input it is ignored in the request and Laravel doesn't validate it despite the fact that it is added to the ruleset with the required validation rule. Empty text inputs are being validated correctly.
+request: ParameterBag {#42 ▼
#parameters: array:1 [▼
"text_input" => ""
"_token" => "TCDqEi2dHVQfmc9HdNf8ju1ofdUQS6MtDBpUMkl7"
]
}
As you can see, the select_input is missing from request parameters if it was left empty.
Here is the HTML code for my select input:
<select class="form-control" name="select_input">
<option disabled selected>Please select...</option>
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
</select>
Is there a way to make the validation work for all fields from the ruleset even if some of them are not present in the request?
From Laravel 5.1 validation documentation:
required
The field under validation must be present in the input data and not empty. A field is considered "empty" is one of the following conditions are true:
The value is null.
The value is an empty string.
The value is an empty array or empty Countable object.
The value is an uploaded file with no path.
P.S. I'm using Laravel 5.1, so present method is not available.

Your html should look like this
<select class="form-control" name="select_input">
<option value="" selected >Please select...</option>
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
</select>
$this->validate($req, [
'text_input' => 'required',
'select_input' => 'required',
]);
If your select box values are integer then
you can use required with integer like
$this->validate($req, [
'text_input' => 'required',
'select_input' => 'required|integer',
]);
Or if you have limited options for that select box then you can use
'select_input' => "required|in:val1,val2,val3",

You made it's option disabled, so it won't send anything through your form.
Change your select box to
<select class="form-control" name="select_input">
<option value="">Please select...</option>
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
</select>

There are few options I can recommend:
Manually validate the request without using the validation extended in the Controller, I.e:
//validator FACADE
$ validator = Validator::make ($request->all(), [
// rules here
]);
By this you can monitor which fields are passed and which one are not passed.
Secondly, set a default value for the select list and check that value when you are validating in the Controller, that is, if you have this default value then nothing is selected. You definitely will have only the fields submitted in your Controller.

Related

how to add a new input as a dropdown type and with filled default options to laravel jetstream registration form

my need is to add a new input as a dropdown type and with filled default options to laravel.8 jetstream registration form.
my problems are:
how to add dropdown input to the registration blade file? use jetstream component or have to create a new component based on livewire or pure laravel component?
where default options for dropdown must be prepare and sent to the blade file?
how can I get selected options from users in app/Actions/Fortify/CreateNewUser.php?
thanks
I understand this is not a full answer to all of your questions but should help.
You can just add a standard select, something like this:
<label for="cars">Choose a car:</label>
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
You can get/use that value in app/Actions/Fortify/CreateNewUser.php accessing the array. The following inserts the new value into the database:
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'car' => $input['cars']),
'password' => Hash::make($input['password']),
'api_token' => Str::random(60),
]);
You will also need to update the app/Models/User.php Model and add the new value as fillable (and of course have added the field to your User Model via a migration):
protected $fillable = [
'name',
'email',
'password',
'car',
];

How can I get only specific elements of an entity with Symfony formbuilder?

In my formbuilder I create a select box from an entity:
$options['choice_label'] = function ( $entity) use ($name) {
if( $entity->getCategory() == null) {
return $entity->getName();
}
};
In the case the Category is NULL, I want to get an option field and if not I do not want an option field.
But what happens is, that in the case the category is not NULL, I get empty option fields, where I am actually do not need an option field at all.
What I get:
<select>
<option>value with category 1</option>
<option>value with category 2</option>
<option>value with category 3</option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
What I need:
<select>
<option>value with category 1</option>
<option>value with category 2</option>
<option>value with category 3</option>
</select>
I see two ways, which will solve your problem.
Use a entity type with a query builder, that passes the right options to your select
https://symfony.com/doc/current/reference/forms/types/entity.html
Use a choice type but write the choices to an array and pass it to your form item
Symfony2 Form Builder - creating an array of choices from a DB query
In case that the query builder throws a error like „could not converted into string“ you will have to define a string-representation for the related entity. You need the string, to tell the typeField which attribute should be displayed in the select.
Here goes the way to solve it for the entity type:
Add a simple choice label
$builder->add('users', EntityType::class, [
// looks for choices from this entity
'class' => User::class,
// select the property which should be used to represent your option in select
'choice_label' => 'username',
]);
Add a more specific choice label
$builder->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => function ($category) {
return $category->getDisplayName();
}
])
Ofc refering to the docs
https://symfony.com/doc/current/reference/forms/types/entity.html

Validation Laravel of 2 fields : first is datepicker and second is select

I have many fields on my form.
2 of them have to bind together.
The first is an datepicker and the second a select field (filled in function of date chosen).
<input class="form-control pull-right" name="day_desired" id="day_desired" placeholder="Day Desired" type="text" value="{{ old('day_desired') }}">
<select id="hour_id" name="hour_id" class="form-control">
<option value="0">--- Choose hour ---</option>
<option value="10:00">10h00</option>
...
</select>
So I want to do this :
If no date chosen, these 2 fields are optionals.
If a date is chosen, the select field is required and not equal to 0 (0 is the first value of options and I use it like a label).
I had many tests with required_if, required_unless no success....
return [
'name'=>'required',
'first_name' =>'required',
...
'day_desired' =>'nullable',
'hour_id' =>'nullable'
];
Thank you very much
The required_if and required_unless checks will look for a certain condition for the value of another field. That is not what you need here.
You can use required_with:
'day_desired' =>'nullable',
'hour_id' =>'required_with:day_desired|nullable|min:1'
The reason that hour_id has to be nullable is that you don't want the min:1 check to run when the day_desired and the hour_id are empty. Still, the required_with rule will fail, even though the field is nullable, as pointed out in this post on GitHub. This behaviour was added in this release: https://github.com/laravel/framework/releases/tag/v5.4.16.

Form input not validate if input is disabled

I have a selection that is disabled is the page not editable is.
Here is my code:
<select name="status" class="form-control" id="status" #if($page->editable == 0) disabled #endif>
<option value="1" #if ($page->status == 1) selected #endif>Online</option>
<option value="0" #if ($page->status == 0) selected #endif>Concept</option>
</select>
But as the input is disabled he send no values with.
And now comes the problem.
I validate the form. Here is my code of validate the form:
$form = $request->validate([
'status' => 'required',
]);
But I get the error now that the value status is empty and required.
Do you now how to set the validation that he only is required is the form is not disabled?
I hope that you can help me.
Firstly, You should use separate function for store and update,
For store, Your validation code seems right, but you can add boolean rule
$form = $request->validate([
'status' => 'required|boolean',
]);
For update, you can use validation rule like this,
$form = $request->validate([
'status' => 'nullable|boolean',
]);
If you are using same function for store and update, you should send value for editable status and you can apply validation rules according editable status like this,
if ($request->editable) {
....
}
else {
...
}
I hope this will help

Laravel validation for a numeric array of HTML Select input

In Laravel, its easy to validate numeric inputs-
$rules = array('numericInput' => 'numeric');
But not sure, how to validate a numeric array. What can be the rule for that. Or is it even possible by Laravel's Validator class?
For e.g-
This HTML form submits multiple Select item to a laravel service
<form .....>
<select multiple="multiple" name="objectIdArr[]" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</form>
What Laravel gets-
( [input] => Array ( [objectIdArr] => Array ( [0] => 1 [1] => 3 [2] => 5 ))
what can be the rule!
Please suggest
Laravel 3.x's validator doesn't handle arrays unfortunately as it expects each unique name to be a string.
You could however extend the validator class as a library (follow the docs for a decent example) and allow your extended class to recieve an array that then is broken out into strings and validated, if any of the strings fail validation the validator returns a failure with your custom message.

Categories