How validation Select Option with extra field ( --please select option--) - laravel

{!! Form::select('occupation', getOccupationStatus() , null, ["class" =>
"form-control"])!!}
<small class="text-danger">{{ $errors->first('status') }}</small>
How validation this field with please select option

You need to give a name to your select field ( in your case it's occupation I think ), then use Validation Rules as below
// Validation rules ...
$rules = [
...
'occupation' => 'required',
...
];

Related

Codeigniter 4 - Apply validation rules without making the fields required for URL

I want to apply validation rules only for the input fields that are not empty (not required) for the URL
For example,
If I submit a form and the input is empty, I got a validation error "Instagram Link must be valid URL.", However, I want it without requiring,
and if the input is not empty, I want to apply the rule "valid_url"
How can we fix it?
if (!$this->validate([
'instagram' => [
'rules' => 'valid_url',
'errors' => [
'valid_url' => 'Instagram Link must be valid url.',
],
],
])){
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
I tried with a permit_link rule, but if I submit it (with an input value like 'mylink' (which is not a valid_url)), it will accept it, but it should not.
Please check the following images and the code:
A form HTML
Result after clicking on edit button
<?= form_open('/settings/edit/1', ['id' => 'setting-form']); ?>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<?= form_label('Instagram'); ?>
<?= form_input(['name' => 'instagram', 'class' => 'form-control', 'id' => 'instagram', 'placeholder' => 'Enter instagram link', 'value' => old('instagram', $setting->instagram)]); ?>
</div>
</div>
</div>
<button type="submit" class="btn btn-dark">Edit</button>
<?= form_close(); ?>
public function edit($id)
{
if (!$this->validate([
'instagram' => [
'rules' => 'permit_empty|valid_url',
'errors' => [
'valid_url' => 'Instagram Link must be valid url.',
],
],
])) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
die('submitted');
}
It should display "Instagram Link must be valid url" and not "submitted",
first
Use separate variable $rules like this :
$rules = ['data' => 'require']
Then
check if $this->request->getVar("instagram"); is empty / is true or etc.. then set it on the $rules
finally
Do Something like this :
$rules = ['extra_data' => 'require'];
if(!empty($this->request->getVar("instagram"))
$rules["instagram"] = "valid_url";
if ($this->validate(rules){
//do something ...
}else {
//do something ...
}
I just noticed that the following examples:
"mylink", "mylink.com", "https://www.mylink.com"
will consider correct for the rule valid_url in Codeigniter (No errors),
While: "https:://www.mylink.com", "mylink#" will apply the validation and the error is applied.

How to resolve Array to String conversion error Laravel

I'm new to laravel and i encountered an array to string conversion error while trying to send tags select form data to sync with my blogs table.
Below is blade snippet that retrieves tags from the database
<div class="form-group">
<label for="tag" class="control-label">Tags</label>
{!! Form::select('tag[]', $tags, old('tag'), ['class' => 'form-control select2', 'multiple' => 'multiple', 'id' => 'add-tag' ]) !!}
</div>
The BlogsController
$blog_data = request()->validate([
'tag.*' => 'required'
]);
blogs = Blog::create( $blog_data );
$blogs->tags()->sync((array)request()->input('tag'));
when i perform a dd on request()->tag
array:2 [▼
0 => "1"
1 => "2"
]
Just use like this
$blogs = Blog::create( $blog_data );
If insert then use like this
$blogs->tags()->attach($request->tag);
If update then use like this
$blogs->tags()->sync($request->tag);
Thank you, i was able to remove this line of code and it worked
tag.*' => 'required'

Why it shows an error in the store() conferece?

I have this code to store a conference. There is a form field that is for the user to select the available payment methods. The form field is like this:
<div class="form-group">
<label for="inputName">
Select the payment methods</label>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_mb" id="payment_mb">
<label class="form-check-label" for="exampleRadios1">
MB
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_c" id="payment_c">
<label class="form-check-label" for="exampleRadios1">
Credit Card
</label>
</div>
</div>
The code to store is like below.
But it shows an error:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value:
'on' for column 'payment_mb' at row 1 (SQL:
insert into `conferences` (`name`, `organizer_id`,
`organizer_name`, `organizer_email`, `invoice_entity`,
`payment_mb`, `updated_at`, `created_at`)
values (a, 2, John W, emailtest#test.com, 1,
on, 2018-07-25 19:17:26, 2018-07-25 19:17:26))
Do you know why?
public function store(Request $request)
{
$conference = Conference::create([
'name' => $request->name,
'city' => $request->city,
'organizer_id' => Auth::id(),
'organizer_name' => $request->organizer_name,
'organizer_email' => $request->organizer_email,
'payment_mb' =>$request->payment_mb ? : 0,
'payment_c' => $request->payment_c ? : 0
]);
$conference->categories()->attach($request->categories);
$conference->save();
Session::flash('success', 'conference created.');
}
The $request->all() shows like:
array:25 [▼
"_token" => ""
"name" => "test"
"organizer_name" => "John W"
"organizer_email" => "..."
"payment_mb" => "on"
"payment_c" => "on"
"invoice_issuer" => "1"
]
Well on wouldn't be an int value. Your request has the value 'on' for that field.
'payment_mb' =>$request->payment_mb ? : 0,
You could maybe try:
'payment_mb' => (int) boolval($request->payment_mb),
Or even just:
'payment_mb' => (bool) $request->payment_mb,
PHP Docs - boolval
You could also actually set a value for the checkbox to be a '1' or something like that.
Some validation might be good.
Update
I think i was looking for filter_var:
filter_var($thatInput, FILTER_VALIDATE_BOOLEAN);
// 'on' => true
// null => false
// 'off' => false
Are you sure this is the correct store code?
payment_c does not appear in the sql statement even though it was set in your code.

How to validate form field with array values[] in laravel?

I'm trying to validate form field with array value.
$this->validate($request, [
'includes' => 'required',
'excludes' => 'required'
]);
HTML is as follows:
<input type="text" name="excludes[]" id="package_exclude"
class="form-control">
<input type="text" name="includes[]" id="package_include"
class="form-control">
The outputted value of $request->includes gives ["Include Value One","Include Value Two"] which is working fine..
But the validation doesn't works..
I suppose you want the arrays to have at least one value? In this case, you can use the min validation rule:
$this->validate($request, [
'includes' => 'required|min:1',
'excludes' => 'required|min:1'
]);

Laravel validation with input field as array "title[]" throws always the same error

No matter which validation rule i brake as long as i have an array notation in input name like this
<div class="form-group">
{!! Form::label('titile', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
</div>
i get this error:
I have tried to use simple plain html input like this
<input type="text" name="title[]" />
and even like this
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required']) !!}
But nothing works.
Only if i make the input field without array notation [] the validation works properly...
my validation rule is this
$this->validate($request, [
'title' => 'required|min:2',
]);
I don't know what else to do, if anyone had similar problem please help.
UPDATE:
i have tried it like this now with only one form input:
<div class="form-group">
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required', 'placeholder' => 'z.B. Deutscher Filmpreis']) !!}
</div>
-
public function rules()
{
$rules = [
];
foreach($this->get('title') as $key => $val)
{
$rules['title.'.$key] = 'numeric';
}
return $rules;
}
Write your validation in individual request file. This ('title' => 'required|min:2') validation does not work for array input. Try this technique for dynamic field validation.
public function rules()
{
$rules = [
];
foreach($this->request->get('title') as $key => $val)
{
$rules['title.'.$key] = 'required|min:2';
}
return $rules;
}
Very Good example at laravel news site.
https://laravel-news.com/2015/11/laravel-5-2-a-look-at-whats-coming/
OK i finally solved this. In Laravel 5.3 something is changed and you can't put empty array brackets for field name.
You must declare indices inside...for example:
this doesn't work
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
but this works
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required']) !!}
UPDATE
So after solving this now i know that if you put empty [ ] brackets this will work if you have multiple input fields with same name...but if you put empty brackets and you have an option to add new fields dynamically like i did...then that single field with empty array brackets will fail because you actually don't have an array...and you must put [0] some indices inside...
and then it works

Resources