Rolin
Rolin

Reputation: 9

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.

Upvotes: 0

Views: 859

Answers (1)

webdevtr
webdevtr

Reputation: 480

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

Upvotes: 2

Related Questions