2016-03-23 72 views
4

我在Laravel中有一個表單,我想根據複選框驗證三個文本字段。它是一個編輯用戶窗體,如果我選擇是,我只想更新密碼。這是我的看法形式:如果選中複選框,有條件驗證laravel

<section> 
     <div class="form"> 
      <h1>Edit: {!! $user->name !!}</h1> 
      {!! Form::model($user, ['method' => 'PATCH', 'action' => ['[email protected]', $user->id], 'files' => true]) !!} 
       <p>Name: {!! Form::text('name') !!} 
       <p>Email: {!! Form::text('email') !!} 
       <p>Update Password? (tick for yes) &nbsp; {!! Form::checkbox('updatepasswordcheck', 0, false) !!} 
       <p>Old Password: {!! Form::password('oldpassword') !!} 
       <p>New Password: {!! Form::password('newpassword') !!} 
       <p>New Password Confirm: {!! Form::password('newpasswordconfirm') !!} 
       @if($user->role_id == 1) 
        <p> User Role: {!! Form::select('user_type', ['admin', 'guest'], '1') !!} 
       @else 
        <p> User Role: {!! Form::select('user_type', ['admin', 'guest'], '0') !!} 
       @endif 
       <p>{!! Form::submit('Update') !!} 
      {!! Form::close() !!} 
      @include ('errors.list_errors') 
     </div> 
    </section> 

然後我有一個名爲EditAccountRequest像這樣的請求:

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class EditAccountRequest extends Request 
{ 
    /** 
    * 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() 
    { 


     if($this->inputs['updatepasswordcheck']) 
     { 
      $rules['oldpassword'] = 'required'; 
      $rules['newpassword'] = 'required'; 
      $rules['newpasswordconfirm'] = 'required'; 
     } 
     $rules['name'] = 'required|min:3'; 
     $rules['email'] = 'required|email'; 

     return $rules; 
    } 
} 

我的問題是,當我看到,如果複選框「updatepasswordcheck」是真實的它永遠不會奏效因爲輸入始終顯示爲空。有人知道怎麼修這個東西嗎?

回答

3

好,我的工作了。我這樣做,它的工作原理:

public function rules() 
{ 
     if($this->has('updatepasswordcheck')) 
     { 
      $rules['oldpassword'] = 'required'; 
      $rules['newpassword'] = 'required'; 
      $rules['newpasswordconfirm'] = 'required'; 
     } 
     $rules['name'] = 'required|min:3'; 
     $rules['email'] = 'required|email'; 

     return $rules; 
} 
2

你可以做到以下幾點:

public function rules(Request $request) 
{ 
    if($request->has('updatepasswordcheck') 
    { 
     $rules['oldpassword'] = 'required'; 
     $rules['newpassword'] = 'required'; 
     $rules['newpasswordconfirm'] = 'required'; 
    } 
    $rules['name'] = 'required|min:3'; 
    $rules['email'] = 'required|email'; 

    return $rules; 
} 
+0

我試着這樣做,我得到這個錯誤:目標[應用程序\ HTTP \請求\請求]不實例化。 – Magearlik

+0

您需要在類的上方以及命名空間下方使用以下內容:'使用App \ Http \ Requests \ Request;'。 – user2094178

+0

沒有,因爲你是在請求對象或東西里面,所以仍然給出錯誤 – Magearlik

1

我使用Laravel 5.5,此代碼工作正常,我的情況是,如果「公司信息」選擇了「是」,然後「公司名稱」應要求下拉。

控制器:

protected function validator(array $data) 
{ 
    return Validator::make($data, [ 

     'company_name' => 'required_if:is_company,1', 

    ]); 
} 

查看:

<label class="control-label">Company Info</label> 
<select id="is_company" class="form-control" name="is_company"> 
     <option value="0">No</option> 
     <option value="1">Yes</option> 
</select> 


<label class="control-label">Company Name</label> 
<input type="text" class="form-control" name="company_name"> 

More Detail