简体   繁体   中英

How to make a field required if it appearing on a form

I have a Laravel 5.8 project and on a Blade I added this:

    @if(empty($user->usr_name))
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <span class='text-danger'>*</span>
                <label>Mobile Number</label>
                <input type="text" class="form-control" name="mobile" value="{{ !empty($user->member->mbr_mobile) ? $user->member->mbr_mobile : old('mobile') }}" required="required">
            </div>
        </div>
    </div>
    @endif
    
    @if(empty($user->usr_email))
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <span class='text-danger'>*</span>
                <label>Email</label>
                <input type="text" class="form-control" name="email" value="{{ !empty($user->usr_email) ? $user->usr_email : old('email') }}" required="required">
            </div>
        </div>
    </div>
    @endif

So if the usr_name of the user is set to NULL, then the user can enter his user name.

And if the usr_email of the user is EMPTY, then he can enter his email address.

If one of these fields are not empty, then it won't be appeared on page.

Now I need to make these fields required as well:

$data = $request->validate([
            'email' => 'required|unique:users,usr_email',
            'mobile' => 'required|unique:users,usr_name',
        ]);

But this is wrong, because if the user has already an username, then the required rule must be omitted and the same applies to user email.

So the question is, how to make a field required if it is appearing on a form? Otherwise it should be nullable .

<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            <span class='text-danger'>*</span>
            @if(empty($user->usr_name))
                <label>Mobile Number</label>
                <input type="text" class="form-control" name="mobile" value="{{ 
                !empty($user->member->mbr_mobile) ? $user->member->mbr_mobile : 
                old('mobile') }}"
            @elseif(empty($user->usr_email))
                <label>Email</label>
                <input type="text" class="form-control" name="email" value="{{ 
                !empty($user->usr_email) ? $user->usr_email : old('email') }}"
            @endif 
            required="required">
        </div>
    </div>
</div>

then

    if($request->email){
       $data = $request->validate([
            'email' => 'required|unique:users,usr_email',
       ]);
    } else {
       $data = $request->validate([
            'mobile' => 'required|unique:users,usr_name',
       ]);
    }

laravel 5 is not support anymore use 8 or higher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM