在php表单中,我希望用户使用选择标签放置性别
在文件Controller中,我要检查字段数据
我的表单代码:
@extends('layouts.app')
@section('title', getOption('app_name') . ' - register')
@section('content')
<style>
/* already defined in bootstrap4 */
.text-xs-center {
text-align: center;
}
.g-recaptcha {
display: inline-block;
}
</style>
<div class="row">
<div class="col-md-3 pull-center"></div>
<div class="col-md-6 pull-center">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Register</h3>
</div>
<div class="box-body">
<form role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<div class="input-group">
<input id="name"
type="text"
class="form-control login-field"
placeholder="@lang('forms.name')"
name="name"
value="{{ old('name') }}">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<div class="input-group">
<input id="email"
type="email"
class="form-control login-field"
placeholder="@lang('forms.email')"
name="email"
value="{{ old('email') }}">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
</div>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<div class="input-group">
<input id="phone"
type="tel"
class="form-control login-field"
placeholder="شماره موبایل"
name="phone"
value="{{ old('phone') }}">
<span class="input-group-addon"><i class="fa fa-phone-square"></i></span>
</div>
@if ($errors->has('phone'))
<span class="help-block" style="color:red">
<strong>{{ $errors->first('phone') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<div class="input-group">
<input id="password"
type="password"
class="form-control login-field"
placeholder="@lang('forms.password')"
name="password">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<div class="input-group">
<input id="password-confirm"
type="password"
class="form-control login-field"
name="password_confirmation"
placeholder="@lang('forms.confirm_password')" >
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
</div>
<div class="form-group">
<div class="input-group">
<select name="gender"
id="gender"
class="form-control login-field"
style="direction:rtl; text-align:right;font-size:12px">
<option value="">Select your gender</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
@if ($errors->has('gender'))
<span class="help-block">
<strong>{{ $errors->first('gender') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('g-recaptcha-response') ? ' has-error' : '' }}">
<div class="text-xs-center">
{!! Captcha::display() !!}
</div>
@if ($errors->has('g-recaptcha-response'))
<span class="help-block">
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">
@lang('buttons.register')
</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
{!! Captcha::script() !!}
@endpush
控制器代码:
<?php
namespace App\Http\Controllers\Auth;
use App\PaymentMethod;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Arcanedev\NoCaptcha\Rules\CaptchaRule;
use SoapClient;
use Illuminate\Http\Request;
use Cache;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
config(["no-captcha.sitekey" => getOption('recaptcha_public_key')]);
config(["no-captcha.secret" => getOption('recaptcha_private_key')]);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'gender' => 'required',
'g-recaptcha-response' => ['required', new CaptchaRule],
],[
'email.regex' => "Your email isn't a valid email",
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
if ( $data['gender'] == 'female'){
$gender = $data['gender'];
}else{
$gender = 'male';
}
return User::create([
'email' => $data['email'],
'gender' => $gender,
'password' => bcrypt($data['password']),
]);
}
public function finish($data){
$user = $this->create($data);
$this->guard()->login($user);
return redirect($this->redirectPath());
}
}
但是现在,当用户选择测试选项时,性别将保存为空! 但是我说如果字段值不是女性,则在此控制器中保存男性 该代码为假的地方?
答案 0 :(得分:0)
在您的控制器中尝试以下代码:
$gender = 'male';
if ( $data['gender'] == 'female'){
$gender = $data['gender'];
}
答案 1 :(得分:-1)
在if语句中,您为$ gender分配了一个额外的)。