13

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?

5 Answers 5

12

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;
}
Sign up to request clarification or add additional context in comments.

Comments

9

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

1 Comment

How if we use checkbox not dropdown?
5

You could do the following in this example:

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;
}

4 Comments

I tried doing this and I got this error: Target [App\Http\Requests\Request] is not instantiable.
You need to have the following above your class and below the namespace, use App\Http\Requests\Request;.
No that still gives an error because you are inside the request object or something
Just use Request::has() if the other doesn't work.
5

A cleaner and straight forward solution

    $validated = $request->validate([
        .
        .
        .
        'newpasswordconfirm' => 'required|accepted',
        'oldpassword' => 'required',
        'newpassword' => $request->newpasswordconfirm === 'yes' ? 'required': 'nullable',
    ]);

Comments

2

may be can use required_unless for this purpose:

$this->validate($request, 
        [
            'content' => 'required_unless:otherfields,null',
        ],
        [
            'content.required_unless' => 'Please enter content for detail'
        ]);

Documentation: https://laravel.com/docs/8.x/validation#rule-required-unless

there is another way to Take a look at the sometimes rule. This will run the rule only when the field is available:

Documentation: https://laravel.com/docs/5.3/validation#conditionally-adding-rules

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.