Description
create request form code:-
public function rules(): array
{
//dd('coming');
return [
'station_id' => ['required',
function ($attribute, $value, $fail) {
if ($value == 0) {
$fail("Please Select Station");
}
}],
'NO' => ['required', 'regex:/^\d{1,3}(\.\d{1,3})?$/'],
'NO2' => ['required', 'between:0,99.99','min:1', 'max:6'],
'NOx' => ['required', 'between:0,99.99','min:1', 'max:6'],
'NH3' => ['required', 'between:0,99.99','min:1', 'max:6'],
'SO2' => ['required', 'between:0,99.99','min:1', 'max:6'],
'CO' => ['required', 'between:0,99.99','min:1', 'max:6'],
'O3' => ['required', 'between:0,99.99','min:1', 'max:6'],
'PM25' => ['required', 'between:0,99.99','min:1', 'max:6'],
'PM10' => ['required', 'between:0,99.99','min:1', 'max:6'],
'airquality' => ['required',
function ($attribute, $value, $fail) {
if ($value == 0) {
$fail("Please Select Airquality");
}
}],
'AQI' => ['required','min:2','max:3'],
//'AQI' => ['required','min:2','max:3','regex:/^\d{3}$/'],
//'AQI' => ['required|numeric|max:3', 'regex:/^\d+(\.\d{1,2})?$/'],
'status' => ['required','boolean']
];
}
public function messages()
{
return [
'NO.required' => 'NO field is required and should be decimal.',
'NO2.required' => 'NO2 field is required and should be decimal.',
'NOx.required' => 'NOx field is required and should be decimal.',
'NH3.required' => 'NH3 field is required and should be decimal.',
'SO2.required' => 'SO2 field is required and should be decimal.',
'CO.required' => 'CO field is required and should be decimal.',
'O3.required' => 'O3 field is required and should be decimal.',
'PM25.required' => 'PM25 field is required and should be decimal.',
'PM10.required' => 'PM10 field is required and should be decimal.',
'AQI.required' => 'AQI field is required and should be decimal.',
];
}
Controller:-
public function store(CreateRequest $request)
{
try{
//$validatedData = $request->validated();
DB::beginTransaction();
Airquality::create([
'station_id' => $request->station_id,
'NO' => $request->NO,
'NO2' => $request->NO2,
'NOx' => $request->NOx,
'NH3' => $request->NH3,
'SO2' => $request->SO2,
'CO' => $request->CO,
'O3' => $request->O3,
'PM25' => $request->PM25,
'PM10' => $request->PM10,
'airquality' => $request->airquality,
'AQI' => $request->AQI,
'status' => $request->status
]);
DB::commit();
}
catch(\Exception $ex){
DB::rollback();
dd($ex->getMessage());
}
session()->flash('alert-success', 'Airquality Created Successfully');
return to_route('airqualities.index');
}
View Code
Hello Sir,
I want to create a form with decimal values. I have created a request form, but it only accepts normal integer values. but i want it to accept only decimal values. Please guide me on what to do. I have attached my code.
View Code:-
<form method="post" action="{{ route('airqualities.store') }}" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col">
<div class="row mb-3">
<div class="col">
<label for="station_id">Select Station</label> <span class="star-color">*</span>
<select class="form-control select2" id="station_id" name="station_id" required>
<option value="">Select Station</option>
@foreach($stations as $station)
<option value="{{ $station->id }}"> {{ $station->name }} </option>
@endforeach
</select>
</div>
<div class="col">
<label for="airquality">Air Quality</label> <span class="star-color">*</span>
<select class="form-control" id="airquality" name="airquality" required>
<option @selected( old('airquality') == '') value="">Select</option>
<option @selected( old('airquality') == 'Good') value="Good">Good</option>
<option @selected( old('airquality') == 'Moderate') value="Moderate">Moderate</option>
<option @selected( old('airquality') == 'Not Obtained') value="Not Obtained">Not Obtained</option>
<option @selected( old('airquality') == 'Poor') value="Poor">Poor</option>
<option @selected( old('airquality') == 'Satisfactory') value="Satisfactory">Satisfactory</option>
<option @selected( old('airquality') == 'Severe') value="Severe">Severe</option>
<option @selected( old('airquality') == 'Very Poor') value="Very Poor">Very Poor</option>
</select>
</div>
<div class="col">
<label for="status">Published</label>
<select class="form-control" id="status" name="status" required>
<option @selected( old('status') == '1') value="1">Publish</option>
<option @selected( old('status') == '0') value="0">Draft</option>
</select>
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="NO">NO</label> <span class="star-color">*</span>
<input type="number" step="0.01" name="NO" class="form-control" id="NO" placeholder="NO" value="{{ old('NO') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="NO2">NO2</label> <span class="star-color">*</span>
<input type="number" name="NO2" class="form-control" id="NO2" placeholder="NO2" value="{{ old('NO2') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="NOx">NOx</label> <span class="star-color">*</span>
<input type="number" name="NOx" class="form-control" id="NOx" placeholder="NOx" value="{{ old('NOx') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="NH3">NH3</label> <span class="star-color">*</span>
<input type="number" name="NH3" class="form-control" id="NH3" placeholder="NH3" value="{{ old('NH3') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="SO2">SO2</label> <span class="star-color">*</span>
<input type="number" name="SO2" class="form-control" id="SO2" placeholder="SO2" value="{{ old('SO2') }}" minlength="3" maxlength="6">
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="CO">CO</label> <span class="star-color">*</span>
<input type="number" name="CO" class="form-control" id="CO" placeholder="CO" value="{{ old('CO') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="O3">O3</label> <span class="star-color">*</span>
<input type="number" name="O3" class="form-control" id="O3" placeholder="O3" value="{{ old('O3') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="PM25">PM25</label> <span class="star-color">*</span>
<input type="number" name="PM25" class="form-control" id="PM25" placeholder="PM25" value="{{ old('PM25') }}" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="PM10">PM10</label> <span class="star-color">*</span>
<input type="number" name="PM10" class="form-control" id="PM10" placeholder="PM10" value="{{ old('PM10') }}" Step=".01" minlength="3" maxlength="6">
</div>
<div class="col">
<label for="AQI">AQI</label> <span class="star-color">*</span>
<input type="number" name="AQI" class="form-control" id="AQI" placeholder="AQI" value="{{ old('AQI') }}" max="999" min="-999">
</div>
</div>
<div class="row mt-3">
<div class="col">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</form>
If you want to validate the float and number you need to use a numeric rule, and also you don't need min and max, it will cater into into two rules, please see the exact syntax below.
~~~
'NO2' => ['required', 'numeric', 'between:0,99.99'],
~~~
Solved
0 Likes 2 Comments
If you want to accept only float, then you need to follow custom regex function, you can add this rule in your app. 'NO2' => ['required', 'regex:/^[0-9]*\.[0-9]+$/']