【问题标题】:Laravel Form Input not required不需要 Laravel 表单输入
【发布时间】:2018-06-08 23:31:29
【问题描述】:

我正在尝试为我的用户制作一个简单的联系表格,如果他们有一个订单 ID 可以将其放入字段中。否则我默认设置为“0”(假设他们有问题)

这是我的代码

Controller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\ContactUS;

class ContactUSController extends Controller
{
    public function contactUS()
    {
        return view('contactUS');
    }

    public function contactUSPost(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required'
        ]);

        ContactUS::create($request->all());

        return back()->with('success', 'Thanks for contacting us! We will get back shortly.');
    }
}



View

    <div class="container">
        <h1>Contact US Form</h1>

        @if(Session::has('success'))
            <div class="alert alert-success">
                {{ Session::get('success') }}
            </div>
        @endif

        {!! Form::open(['route'=>'contactus.store']) !!}

        <div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
            {!! Form::label('Name:') !!}
            {!! Form::text('name', old('name'), ['class'=>'form-control', 'placeholder'=>'Enter Name']) !!}
            <span class="text-danger">{{ $errors->first('name') }}</span>
        </div>

        <div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
            {!! Form::label('Email:') !!}
            {!! Form::text('email', old('email'), ['class'=>'form-control', 'placeholder'=>'Enter Email']) !!}
            <span class="text-danger">{{ $errors->first('email') }}</span>
        </div>

        <div class="form-group">
            {!! Form::label('Order ID:') !!}
            {!! Form::number('orderId', old('orderId'), ['class'=>'form-control no-spinners', 'placeholder'=>'Do you have and order id?']) !!}

        </div>

        <div class="form-group {{ $errors->has('message') ? 'has-error' : '' }}">
            {!! Form::label('Message:') !!}
            {!! Form::textarea('message', old('message'), ['class'=>'form-control', 'placeholder'=>'Enter Message']) !!}
            <span class="text-danger">{{ $errors->first('message') }}</span>
        </div>

        <div class="form-group">
            <button class="btn btn-success">Contact US!</button>
        </div>

        {!! Form::close() !!}

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ContactUS extends Model
{
    public $table = 'contact_us';

    public $fillable = ['name','email','message'];
}

Migration

class CreateContactUsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact_us', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->text('message');
            $table->integer('orderId')->default('0');
            $table->boolean('solved')->default(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contact_us');
    }
}

所以问题是,当我没有在字段 OrderId 上输入任何值时,它采用默认值 0 但是,如果我输入任何数值 1 or 2 etc 它仍然会在我的数据库中保存 0

知道为什么会产生这种反应吗?

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    正如 Deepak 在 cmets 中所说,您可以在控制器中检查 null 的值:

    $data=$request->all();
    $data['orderId'] = $data['orderId'] == '' ? 'No Order ID Entered' : $data['orderId'];
    

    或者,传递一些虚假的东西

    $data=$request->all();
    $data['orderId'] = $data['orderId'] == '' ? 0 : $data['orderId'];
    

    【讨论】:

      【解决方案2】:

      允许对模型中的 orderId 字段进行质量分配,

       public $fillable = ['name','email','message','orderId'];
      

      【讨论】:

      • 感谢您的回答,但现在我有另一个问题。 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'orderId' cannot be null (SQL: insert into contact_us` (name, email, orderId, message, updated_at, created_at) 值 (asd, asd@gormail.com, , asd, 2017-12-28 12: 2017-12-28 11:03 12:11:03))`
      • 这是因为通过表单传递了空(null)值
      • @SagarGautam 好的,我明白,但我认为这没关系,因为我在 orderId 列中设置了默认值!不是吗?
      • 在插入语句中不包含列但在接受时发送 null 时将使用默认值
      • @KwnstantinosNatsios 如果您不传递列的键值对,则将考虑默认值,但如果您传递键和空值对,则会抛出此异常。希望你能理解
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 2022-01-24
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      相关资源
      最近更新 更多