Home  >  Q&A  >  body text

There is a problem with data validation

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

JackieJackie1842 days ago1286

reply all(3)I'll reply

  • 卢小强

    卢小强2019-10-08 12:00:41

    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

    reply
    0
  • 卢小强

    卢小强2019-10-08 11:58:05

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

    reply
    0
  • Jackie

    Already solved, write post as port

    Jackie · 2019-10-08 18:19:24
  • Cancelreply