Laravel validation Error messages to string

时光总嘲笑我的痴心妄想 提交于 2019-12-03 16:19:01

You should do like this :

$errorString = implode(",",$validator->messages()->all());

P.S. Assuming

$validator = Validator::make($dataToBeChecked,$validationArray,$messageArray)

The $validator->errors() returns a MessageBag,

see: https://laravel.com/api/5.3/Illuminate/Support/MessageBag.html.

You are close, you need to call the getMessages() function on errors(), so:

foreach ($validator->errors()->getMessages() as $key => $value) {

Hope this helps :)

You are not converting validation errors to array.Please use the below function and pass validation errors as parameter.

 public function validationErrorsToString($errArray) {
        $valArr = array();
        foreach ($errArray->toArray() as $key => $value) { 
            $errStr = $key.' '.$value[0];
            array_push($valArr, $errStr);
        }
        if(!empty($valArr)){
            $errStrFinal = implode(',', $valArr);
        }
        return $errStrFinal;
    }
//Function call.
$result = $this->validationErrorsToString($validator->errors());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!