dongxie3701 2017-11-24 14:54
浏览 19
已采纳

在laravel中的db中验证并提交表单

I want to submit data into db when the user fills a form. But it is behaving strangely.

This is my contoller function:

      namespace App\Http\Controllers\PrivatePages;
      use App\Http\Controllers\Controller;
      use App\Http\Requests\Request\CreateProductRequest;
      /*use App\Productuse Illuminate\Support\ServiceProvider;*/
      use Illuminate\Support\Facades\View;
      use Illuminate\Support\Facades\DB;
      use App\Http\FormRequests;


    public function UpdateAdminProfile(CreateProductRequest $request){
    Product::StoreProductDetails($request);
  }

This is my model function which defines the funtion:

   namespace App;

   use App\Http\Requests\CreateProductRequest;
   use Illuminate\Database\Eloquent\Model;
   use App\Http\Controllers\PrivatePages;



  use  Illuminate\Http\Request;

public static function StoreProductDetails(CreateProductRequest $request)
{

        $saveproduct = new Product();
        $saveproduct->name = $request->get('name');
    $saveproduct->description = $request->get('description');
    $saveproduct->save();

}

this is my request file:

<?php
     namespace App\Http\Requests\Request;
    use Illuminate\Foundation\Http\FormRequest;
    class CreateProductRequest extends FormRequest
 {
 /**
  * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{

    return [

        'name' => 'required',
        'description' => 'required',
        'contactinfo'=> 'required',

    ];

}



   public function messages()
   {
      return[
        'name.required' => 'Enter Name',
        'description.required' => 'Enter description',
        'contact.required' => 'Enter contact info',

    ];
    }
  }

my form

   @if (Session::has('success'))
   <div class="alert alert-success" role="alert" style="font-size: 18px;">
    <strong>Success: </strong>

    {{ Session::get('success') }}
 </div>
  @endif

       <form id="" method="post" class="form-horizontal" action="{{ 
      route('updateadminprofile')}}"enctype="multipart/form-data" >



      <div class="form-group">
      <label class="col-sm-4 control-label" for="userName"> Name</label>
      <div class="col-sm-5">
     <input type="text" class="form-control" id="name" name="name" 
      placeholder="name}"  value="name" />
     </div>
    </div>
   <div class="form-group">
   <label class="col-sm-4 control-label" for="userName"> Description</label>
   <div class="col-sm-5">
    <input type="text" class="form-control" id="description" 
   name="description" placeholder="Description}"  value="description" />
  </div>
  </div>
     <button  style="margin-left: 30%" type="submit" class="btn btn-primary" 
       name="signup" value="sumbmit" >Save</button>

It is not giving error

 jsut refreshes the page and no record is saving in db

Why is it throwing an error as every thing seems good? Now it just Refreshes the page. not even going to route specified in action of form

  • 写回答

2条回答 默认 最新

  • dongyongju9560 2017-11-24 16:20
    关注

    Change Your CreateProductRequest to the following by extending FormRequest not Request

    use Illuminate\Foundation\Http\FormRequest;
    
    class CreateProductRequest extends FormRequest
    {
    public function rules()
    {
        return [
            'name' => 'required',
            'description' => 'required',
            'contactinfo'=> 'required',
        ];
    }
    
    public function messages()
    {
        return [
            'name.required' => 'Enter Name',
            'description.required' => 'Enter description',
            'contact.required' => 'Enter contact info',
        ];
    }
    }
    

    Hope this works :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题