Laravel 5.3 - Validation - laravel

I'm trying validate my form specified for multi language.
<form>
<select name="test">
<option value="1">YES</option>
<option value="2">NO</option>
</select>
<input type="text" name="lorem[1]">
<input type="text" name="ipsum[1]">
<input type="text" name="lorem[2]">
<input type="text" name="ipsum[2]">
</form>
I have the code to the validation like this
$validator['test'] = "required|integer";
$validator['lorem.*'] = "required_with_all:ipsum.*|min:3";
$validator['ipsum.*'] = "required_with_all:lorem.*|min:3";
$this->validate($request, $validator);
Field 'lorem.' must have value if the field 'ipsum. have value and conversely.
Everything works fine except for one case.
If I don't fill any fields except 'test'.
It isn't good valid:
test = '1';
lorem[1] = '';
ipsum[1] = '';
lorem[2] = '';
lorem[2] = '';
How I can repair it?

Related

How to avoid livewire request for simple inputs calculations?

How would I avoid doing calculation for 3 inputs using Livewire and use JS instead, but still can bind the inputs and their new values with the component.
Example:
<input id="cash-price"
type="text"
wire:model="total_cache_price"
class="amount-credit">
<input id="deposit"
type="text"
wire:model="deposit"
class="amount-credit">
<input id="trade-in"
type="text"
wire:model="trade_in"
class="amount-credit">
I can easily do a simple calculation using JS, but the properties in Livewire component would still be empty or null after submitting the form. I am trying to avoid livewire requests for every input change.
Note: I understand the deferred updating in livewire, the problem is with the property values not changing.
I will show you an example in alpine js + livewire way.
Here I want to set value in two inputs from a selected option in select.
Please note that x-ref is used in alpine to directly access DOM elements without using getElementById.
<div x-data="{
from_date : #entangle('from_date').defer,
to_date : #entangle('to_date').defer,
dateChange(){
select = this.$refs.years;
this.from_date = select.options[select.selectedIndex].getAttribute('data-from');
this.to_date = select.options[select.selectedIndex].getAttribute('data-to');
},
resetFilters(){
this.net_balances = false;
}}">
<select x-ref="years" #change="dateChange()">
<option value="">Year</option>
#foreach ($years as $key => $year)
<option wire:key="{{ 'years'.$key }}" data-from="{{ $year->from_date }}" data-to="{{ $year->to_date }}" value="{{ $year->id }}">{{ $year->name }} </option>
#endforeach
</select>
<div>
<text type="date" x-model="from_date" />
</div>
<div >
<text type="date" x-model="to_date" />
</div>

Laravel 7 - Select only one row from a database and pass it to blade

I am trying to pass a row from database to blade. But I am getting the error message stating
Undefined variable : tuser
Blade(multipage1)
//Other Codes
<input type="text" class="form-control" id="email" name="email" placeholder="Enter email" value="#if($tuser){{$tuser->temp_email}}#endif">
Controller
$tuser = DB::table('temp_users')->where('referenceno',$reference)->first();
if(!(is_null($tuser))){
return view('multipage1',compact('tuser'));
}
I also tried using
$tuser = DB::table('temp_users')->where('referenceno',$reference)->first();
if(!(is_null($tuser))){
return view('multipage1')->with('tuser',$tuser);
}
But I am getting the same error
Best approach is :
Controller
$tuser = DB::table('temp_users')->where('referenceno',$reference)->first();
return view('multipage1',compact('tuser'));
notice the compact, I passed the variable name as string and without the $ sign
and your View
//Other Codes
<input type="text" class="form-control" id="email" name="email" placeholder="Enter email" value="{{$tuser->temp_email ?? ''}}">
// First Create Model
// Using Eloquent Query
$tuse = TempUsers::where('referenceno',$reference)->first();
return view('multipage1',compact('tuser'));

Populate input field from a dropdown using codeigniter

chapter_number | chapter_title |
1 | Introduction |
2 | Number System |
<select type="text" name="chapter_number" class="form-control form-control-line" value="">
<option selected="option" disabled="selected">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select
<input type="text" name="chapter_title" id="title" class="form-control"/>
above is my table and next is my code the select option and the input field. I want that when I select '1' automatically the input field will print chapter title 'Introduction' and If I select '2' the input field will became 'Number System'.
If you want to get data from database you need to call ajax on onchange event of select box.
Add "setinput" class to select box
<select type="text" name="chapter_number" class="form-control form-control-line setinput" value="">
Add below script
$('body').on('change','.setinput',function(){
$.ajax({
url: 'your_base_url/controller_name/your_method',
type:'POST',
data:{id:$(this).val()},
success:function(result){
$('input[name="chapter_title"]').val(result);
}
})
});
Controller method would be like below
public function your_method(){
$id = $this->input->post('id');
$this->db->select('*');
$this->db->from('database_table_name');
$this->db->where('chapter_number',$id);
$result = $this->db->get()->row();
echo $result->chapter_title;
}
I hope, it would be helpful to you.

Using Webmatrix, how do I save data not displayed in the select in a foreach loop?

I am desperate! I have a list of people with names and a unique ID. I need to display just the first and last name in a drop down list and then when the user selects the name they want from the list and submits, I would like to create a new record in a different table and add:
first name
last name
unique ID
I just don't know the syntax to do it. I have tried everything and this is the closest I get which works for one record in the list but multiple records in the list fails.
<select class="form-control" name="">
<div>#foreach(var Team in MyTeam){
<option value="">#Team.FirstName #Team.LastName </option>
<input type="hidden" name="FirstName" value=#Team.FirstName>
<input type="hidden" name="LastName" value=#Team.LastName>
<input type="hidden" name="TeamID" value=#Team.UserID>
<input type="hidden" name="ReferrerName" value=#Team.UserID>
}
</div>
You do not need the hidden fields, they will not work.
<select class="form-control" name="formUser">
#foreach(var Team in MyTeam){
<option value="">#Team.FirstName #Team.LastName </option>
}
</select>
Then, you need to add post code
if(IsPost){
var userID = Request["formUser"];
var sqlselect = "SELECT * FROM YourTable Where UserID = #0";
var sqldata = db.QuerySingle(sqlselect, userID);
var firstname = sqldata.FirstName;
var lastname = sqldata.LastName;
//And so on to get your variables then you can use them to insert
}

Using Webmatrix, drop down menu to post matched data into text box

I have drop down menu which i choose one item from it and then by using onblur event i can post the match detail into input text box. whenever i choose from the drop menu, the matched detail should posted into the input text box. In my example i will choose a specific industry field and the related questions should be appear in question1 and question2 input text box. Example is below:
var db = Database.Open("me");
var ind = Request["areagroup"];
var sqlq = "SELECT id,indName,question1,question2 from Industry where id = #0";
var data = db.Query(sqlq,ind);
<select name="areagroup" onblur="???? ">
<option value="0">General</option>
<option value="1">Services</option>
<option value="2">Basic Materials</option>
</select>
<label>Question 1</label><input type="text" name="q1" />
<br/>
<label>Question 2</label><input type="text" name="q2" />
I'm not sure of your target.
The code that follows populates the input fields when the dropdown loses its focus:
#{
var question1 = "";
var question2 = "";
if (IsPost)
{
if(Request["butt"] != "test"){
var db = Database.Open("me");
var ind = Request["areagroup"];
var sqlq = "SELECT id,indName,question1,question2 from Industry where id = #0";
var data = db.Query(sqlq,ind);
question1 = data.question1;
question2 = data.question2;
} else {
// If submit button is pressed
}
}
}
<form method="post">
<select name="areagroup" onblur="this.form.submit()">
<option value="0">General</option>
<option value="1">Services</option>
<option value="2">Basic Materials</option>
</select>
<br/ >
<label>Question 1</label><input type="text" name="q1" value="#question1" />
<br/>
<label>Question 2</label><input type="text" name="q2" value="#question2" />
<br/>
<input type="submit" name="butt" value="test" />
</form>
I have provided a submit button and an if statement inside the IsPost block to test if the form is submitted by the user.
In my opinion, a better solution is to use the onchange instead of the onblur event adding a dummy option to the dropdown (something like <option value="-1">--- Please select ---</option>).

Resources