dongpiansui8755 2014-10-19 10:22
浏览 43

从ViewErrorBag动态获取MessageBag?

In my app, I have a generic place for errors in the view that I display simply using:

show_messages($errors->getMessages());

In the show_messages function I parse out the errors in the way I would like them displayed. However I started using some grouping in my forms like register[email]. When I display the errors I use ->withErrors(Input::get('register'), 'register').

But now I can't dynamically get the messages becasue $errors->getMessages() goes to default message bag by default (which in this case is empty).

I can get the errors using $errors->register->getMessages(), but that's assuming I know it will be register. Looking at the ViewErrorBag class I don't see any methods to provide the bag. Is there anyway to do this without altering the current class to get all the bags dynamically or all the messages in each bag dynamically?

  • 写回答

2条回答 默认 最新

  • dpbtbcz6650 2014-10-19 16:28
    关注

    You could simply iterate over all of the object properties.

    foreach ($errors as $errorBag) {
        show_messages($errorBag->getMessages());
    }
    

    While theoretically you would need to check for the property type to know if it's a MessageBag, MessageBag doesn't have any other public properties.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法