Reputation: 221
Laravel Nullable is not working, i try everything but it still not working. Please Look My Code..
Controller :-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Pop;
class PopController extends Controller
{
public function index(){
return view('test2');
}
public function create(Request $request){
$formvalidtion = $request->validate([
'usercode' => ['nullable', 'required'],
]);
return "<h1>SUCCESS</h1>";
}
}
Model :-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pop extends Model
{
protected $timestamps = false;
protected $fillable = ['usercode'];
}
Migration :-
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePopsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('usercode')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pops');
}
}
Route :-
route::get('test', 'PopController@index');
route::post('testcheck', 'PopController@create')->name('uc');
View :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Nullable Test</title>
</head>
<body>
<h1>Test Nullable</h1>
<form action="{{ route('uc') }}" method="POST">
@csrf
<input type="number" name="usercode" id="" placeholder="Enter User Code">
@error('usercode')
{{ $message }}
@enderror
<br>
<input type="submit" >
</form>
</body>
</html>
I spend 1 hour to find solution of but not get. some of youtube video in mentioned that datatype of field must be int and nullable . this also not working is there any solution available?
Upvotes: 1
Views: 3561
Reputation: 9029
It's not right to use nullable
and required
validations rules together:
required
: The field under validation must be present in the input data and not empty.nullable
: The field under validation may be null.present
: The field under validation must be present in the input data but can be empty.filled
: The field under validation must not be empty when it is present.prohibited
: The field under validation must be empty or not present.sometimes
: The field under validation will only be validated if it is present.exclude
: The field under validation will be excluded from the request data returned by the validate
and validated
methods.You may use nullable
and present
together:
$formvalidtion = $request->validate([
'usercode' => ['nullable', 'present'],
]);
Upvotes: 4
Reputation: 11414
You don't specify what you mean with "not working" (i.e. what you expect, but what happens instead), but I'm assuming you mean that null values are not accepted by the validation.
That would be because the required
rule expects a value to be non-empty. So your nullable
rule probably passes, but required
fails.
If you want a value to be present, but not necessarily non-empty, you can use the present
rule.
Upvotes: 1