这是我的注册控制器,注册方法:
public function register(StoreUser $request)
{
$user = $this->create($request->all());
$user = User::findOrFail($user->id);
Mail::to($user->email)->queue(new EmailVerification($user));
return view('auth.verify-email');
}
以下是StoreUser请求:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use App\Rules\allAreSpaces;
class StoreUser extends FormRequest
{
/**
* 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()
{
return [
'email' => 'required|email|max:64',
'phone' => 'bail|required|numeric|phone_min|phone_max|unique:users|not_in:0',
'password' => [new allAreSpaces, 'required', 'min:8', 'max:16', 'confirmed'],
'password_confirmation' => 'required',
];
}
public function messages()
{
return [
'email.required' => 'Email address cannot be empty',
'email.email' => 'Enter a valid Email address',
'email.max' => 'Email address cannot exceed 64 characters',
'email.unique' => 'Email already exists',
'phone.required' => 'Mobile number cannot be empty',
'phone.numeric' => 'Mobile number has to be numeric',
'phone.phone_min' => 'Mobile number should contain a minimum 5 characters',
'phone.phone_max' => 'Mobile number cannot exceed 11 characters',
'phone.unique' => 'Mobile number already exists',
'phone.not_in' => 'Enter a valid mobile number ',
'password.required' => 'Password cannot be empty',
'password.min' => 'Password should contain minimum 8 characters',
'password.max' => 'Password cannot exceed 16 characters',
'password.confirmed' => 'Password mismatch.Retry',
'password_confirmation.required' => 'Confirm Password cannot be empty',
];
}
public function withValidator(Validator $validator)
{
$email = $this->request->get( 'email' ); // Start with
$user = \App\User::Where('email', $email)->first();
if($user && $user->activated == 0){
$row = \App\User::find($user->id);
$row->delete();
}else{
$validator->sometimes('email', 'unique:users', function ($input) {
return true;
});
}
}
}
这是名为'allAreSpaces'的自定义规则:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class allAreSpaces implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if(strlen($value) > 0){
if (strlen(trim($value)) == 0){
return false;
}
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Password cannot contain space character alone';
}
}
我想要的是如果用户只键入所有密码字段中的空格,我想抛出'密码不能单独包含空格字符'消息。 当用户没有输入任何内容时,它正在工作。如果他们输入的空间不起作用。
我怎样才能实现这一目标?
如果所有都是空格,还有其他简单的方法来显示错误消息吗?
答案 0 :(得分:1)
@ foo.ar,
在App\Http\Kernel.php
中,您可以看到应用了TrimStrings
个中间件。它将修剪所有空白区域。如果你只在任何输入中输入空格,它将修剪这些空格,你最终将没有输入任何值。
所以如果你想验证空格,你必须在
中添加以下代码<强> \应用\ HTTP \中间件\ TrimStrings.php 强>
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array
*/
protected $except = [
'password',
'password_confirmation',
];
}