Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional validation if checkbox selected laravel

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) &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>

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?

like image 673
Magearlik Avatar asked Mar 23 '16 11:03

Magearlik


2 Answers

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;
}
like image 85
Magearlik Avatar answered Oct 21 '22 15:10

Magearlik


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

like image 26
Muhammad Shahzad Avatar answered Oct 21 '22 15:10

Muhammad Shahzad