简体   繁体   English

如果出现在表单上,如何使字段成为必填字段

[英]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:我有一个 Laravel 5.8 项目,在 Blade 上我添加了这个:

    @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.所以如果用户的usr_name设置为NULL,那么用户可以输入自己的用户名。

And if the usr_email of the user is EMPTY, then he can enter his email address.如果用户的usr_email是 EMPTY,那么他可以输入他的 email 地址。

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.但这是错误的,因为如果用户已经有用户名,则必须省略所需的规则,这同样适用于用户 email。

So the question is, how to make a field required if it is appearing on a form?所以问题是,如果一个字段出现在表单上,如何使它成为必填字段? Otherwise it should be nullable .否则它应该是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 laravel 5 不再支持使用 8 或更高

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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