There is a problem with data validation
Jackie
Jackie 2019-10-03 23:55:41
0
3
1350

There is a problem with my data validation. When each field is empty, it prompts "The user name cannot be empty." In addition, when all fields have values, it also prompts "The user name cannot be empty." What is the reason?

03.png04.png05.png

Jackie
Jackie

reply all(2)
卢小强

What does your 'message'=>$res mean? Assign validate to message

public function addUser(Request $request)
{
    $data = $request->param();
    $status = 0;
    $message = '';
    $rule = [
        'hotel_name|用户名' =>"require",
        'phone|手机号' => 'require|number|max:11|min:7',
        'address|地址'=>'require',
        'name|联系人'=>'require|chs',
        'captcha|验证码' => 'require|captcha'
    ];
    $msg =[
        'phone.number'=>'手机号必须是数字',
        'phone.max'=>'手机号不能超过11位',
        'phone.min'=>'手机号不能低于7位',
        'name.chs'=>'联系人必须是汉字'
    ];
    $result = $this->validate($data, $rule, $msg);
    if (true ===$result) {
        $map = [
            'phone' => $data['phone']
        ];
        $check = UserModel::get($map);
        if (null === $check) {
            $user = UserModel::create($request->except('captcha'));
            if (null === $user) {
                $status = 0;
                $result = '添加失败~~';
            } else {
                $status = 1;
                $result = '添加成功请登录';
            }
        } else {
            //如果在表中查询到该用户名
            $status = 0;
            $result = '用户名重复,请重新输入~~';
        }
    }
    return ['status'=>$status, 'message'=>$result,'data'=>$data];
}

This is my user registration. You can refer to it

卢小强

The content of the from form is also sent out to take a look

  • reply Already solved, write post as port
    Jackie author 2019-10-08 18:19:24
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template