Laravel 5验证 - 表单元素

时间:2015-11-05 07:13:17

标签: php css laravel laravel-4 laravel-5

我的验证表单工作正常,看起来像这样:

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ')) !!}
{{$errors->first('email')}}

如果电子邮件不好,我收到此错误:已收到电子邮件。

问题是我不想在输入上放置一个css类,如error,以便为输入添加红色背景边框。

我该怎么做?

7 个答案:

答案 0 :(得分:7)

  

您可以使用if条件进行检查错误:

 <div class="form-group {{ $errors->has('email') ? 'has-error' :'' }}">
   {!! Form::text('email',null,['class'=>'form-control','placeholder'=>'Email Address']) !!}
   {!! $errors->first('email','<span class="help-block">:message</span>') !!}
</div>

答案 1 :(得分:3)

这是我的正确答案,不使用其他div's

{!! Form::email('email', null, $attributes = $errors->has('email') ? array('placeholder' => 'Email', 'class' => 'form-control has-error') : array('placeholder' => 'Email', 'class' => 'form-control')) !!} 
{{$errors->first('email')}}

答案 2 :(得分:3)

您可以使用以下代码检查电子邮件字段是否有任何验证错误

 <div class="form-group has-feedback @if($errors->has('email') has-error @endif">
        <input name="email" class="form-control"/>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
        @if ($errors->has('email')) <p class="help-block"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
            {{ $errors->first('email') }}</p>
        @endif
    </div>

答案 3 :(得分:1)

您可以随意添加HTML并根据需要设置样式。

HTML:

<span class="error">{{$errors->first('email')}}</span>

CSS:

.error {
    border: 1px solid red;
}

编辑:将has-error类添加到输入本身,如下所示:

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ' . $errors->first('email', 'has-error'))) !!}

答案 4 :(得分:0)

添加自定义错误消息

'custom' => array(
    'email' => array(
        'required' => 'We need to know your e-mail address!',
    ),
),

Custom Error Messages in Laravel

答案 5 :(得分:0)

<script> 
  if({{$errors->has('email')}}){
    $(input[type="email"]).css('border-color', 'red');
  }
</script>

答案 6 :(得分:-1)

将类包含错误添加到您的表单中

import wx
import wx.lib.buttons as buttons

class Main(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, id=-1, title=title, size=(300, 300))
        self.initUI()
        self.panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, size=(10000, 10000))
        self.backGroundImage=''
        self.Layout()

    def initUI(self):
        menubar = wx.MenuBar()
        fileMenu = wx.Menu()
        fileMenu.AppendSeparator()

        imp = wx.Menu()
        importBackgroundButton = imp.Append(wx.ID_ANY, 'Import background')
        self.Bind(wx.EVT_MENU, self.OnImportBackground, importBackgroundButton)
        fileMenu.AppendMenu(wx.ID_ANY, 'I&mport', imp)
        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)
        self.SetTitle('test')
        self.Centre()
        self.Show(True)

    #load background
    def OnImportBackground(self, e):
        app = wx.App(None)
        style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
        dialog = wx.FileDialog(None, 'Open', wildcard='*.png', style=style)
        if dialog.ShowModal() == wx.ID_OK:
            path = dialog.GetPath()
        else:
            path = None
        dialog.Destroy()
        self.backgroundImage = ButtonImage(self, self.panel, path, (0, 0))
        W = self.backgroundImage.bmp.GetSize()[0]
        H = self.backgroundImage.bmp.GetSize()[1]
        self.SetSize((W+16, H+58))
        self.Refresh()
        #crash


class ButtonImage():
    def __init__(self, parent, panel, nameImage, pos):
        self.panel = panel
        self.bmp = wx.Bitmap(nameImage, wx.BITMAP_TYPE_ANY)
        self.maxPiecePositionX = self.panel.GetSize()[0] - self.bmp.GetSize()[0]
        self.maxPiecePositionY = self.panel.GetSize()[1] - self.bmp.GetSize()[1]
        self.bmapBtn = wx.BitmapButton(self.panel, id=wx.ID_ANY, bitmap=self.bmp, style=wx.NO_BORDER, pos=pos)
        self.bmapBtn.Bind(wx.EVT_LEFT_DOWN, self.OnClickDown, self.bmapBtn)
        self.bmapBtn.Bind(wx.EVT_LEFT_UP, self.OnClickUp, self.bmapBtn)
        self.bmapBtn.Bind(wx.EVT_MOTION, self.MoveButton, self.bmapBtn)
        self.hold = 0
        self.holdPosition = (0, 0)

    def EnterButton(self, event):
        pass

    def LeaveButton(self, event):
        self.hold = 0

    def OnClickDown(self, event):
        obj = event.GetEventObject()
        self.hold = 1
        self.holdPosition = (event.GetX(), event.GetY())

    def OnClickUp(self, event):
        self.hold = 0

    def MoveButton(self, event):
        deltaX, deltaY = 0, 0
        if self.hold:
            deltaX = event.GetPosition()[0] - self.holdPosition[0]
            deltaY = event.GetPosition()[1] - self.holdPosition[1]
            newPositionX = self.bmapBtn.GetPosition()[0] + deltaX
            newPositionY = self.bmapBtn.GetPosition()[1] + deltaY
            if (0 < newPositionX < self.maxPiecePositionX) and (0 < newPositionY < self.maxPiecePositionY):
                self.bmapBtn.SetPosition((newPositionX, newPositionY))
            else:
                self.holdPosition = self.holdPosition[0] + deltaX, self.holdPosition[1] + deltaY
            self.bmapBtn.Raise()
            self.bmapBtn.Refresh()

app = wx.App()
frame = Main(None, "Test")
frame.Show()
app.MainLoop()

如果出现错误,可以使用if条件,在

中设置类
{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}

或者你可以这样做

@if($errors->has('email'))
    {!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}
@else
    {!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control')) !!}
@endif