I have a form in Laravel in which I want to validate three text fields based on a checkbox. Its an edit user form in which I only want to update the password if I select yes. This is the form in my view:
<section>
    <div class="form">
        <h1>Edit: {!! $user->name !!}</h1>
        {!! Form::model($user, ['method' => 'PATCH', 'action' => ['AccountController@update', $user->id], 'files' => true]) !!}
        <p>Name: {!! Form::text('name') !!}
        <p>Email: {!! Form::text('email') !!}
        <p>Update Password? (tick for yes)   {!! 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>
I then have a request called EditAccountRequest like so:
<?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;
    }
}
My problem is that when I see if the checkbox 'updatepasswordcheck' is true it never works because the input always appears as null. Anyone know how to fix this?
Ok I worked it out. I did this and it works:
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;
}
                        I'm using Laravel 5.5 and this code works fine, my scenario was if "Company Info" drop-down selected "Yes" then "Company Name" should be required.
Controller:
protected function validator(array $data)
{
   return Validator::make($data, [
      'company_name' => 'required_if:is_company,1',
   ]);
}
View:
<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With