结合验证日期和非验证数据以存储在laravel中

时间:2018-07-26 06:04:47

标签: php laravel validation store laravel-5.6

在这里,我有两个输入字段,分别是姓名和年龄。

我想验证姓名,但不验证年龄

如何将这两个值都发送给存储区。

request()->validate([
        'name' => 'required',
]);

//Here i wanna add 'age' along with 'name' in $request
//Then i must store.

Pastor::create($request->all());

3 个答案:

答案 0 :(得分:1)

$request->all()已发送您请求中的所有内容。但是,如果愿意,您可以专门传递请求值来创建如下方法:

Pastor::create([
   'name'=> $request->name,
   'age' => $request->age
   //any other fields
]);

我假定您已经像输入传递给create方法那样命名了输入,并且应该可以使用。

答案 1 :(得分:0)

使用Laravel的Validator类。假设您有UserController

Use Validator;
Use App\User;

class UserController extends Controller
{
    // Your Validator
    protected function yourValidatorName(array $data)
    {
        $rules = ['name'=>'required'];
        return Validator::make($data,$rules);;
    }

    // Your Main Function
    public function yourFunctionName(Request $request)
    {
        $isValid = $this->yourValidatorName($request->all());
        if(!isValid->fails())
        {
            // your code here (validation passed)
        }
        else
        {
             // your code here (validation failed)
        }
    }
}

答案 2 :(得分:0)

您只需在名称字段上打勾

service_description[[:blank:]]*check2\|service_description[[:blank:]]*check10

没有自定义消息

$this->validate($request,[
        'name' => 'required|min:3|max:200',
    ],[
        'name.required' => 'name is a required field.',       // custom messages you can omit them 
        'name.min' => ' Name must be at least 3 characters.',  // custom message for minimum characters 
        'name.max' => ' Name should not be greater than 200 characters.', // custom message for maximum characters 
    ]);
Pastor::create($request->all());