如果出现错误,则阻止模态关闭,输入将保留

时间:2014-12-12 07:46:00

标签: php laravel modal-dialog

这就是我想要的。当我点击" Save"按钮,如果有错误,我的模态不会消失。我的意见仍然存在。

如果注册成功,我的模态将会消失

我这里有代码并使用Laravel。

这是视图和控制器

查看页面

{{ Form::open(array('url'=>'main/branches')) }}
<div class="modal fade" id= "myModalCreate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title"><center>Create Branch</h4>
        </div>
    <div class="modal-body">
    @if(Session::has('error'))
        <div>{{ Session::get('error') }}</div>
    @endif
    <table>
        <tr>
            <td style="padding:10px;"><label>Branch Name</label></td>
            <td width="10" valign="top" style="padding:10px;"><b>:</td> 
            <td><input type="text" class="form-control" name="branch_name" id="branch_name" pattern="[a-zA-Z0-9- ]{5,30}" onkeyup="autocap(this.id)" maxlength = "30" title="Accepts only letters, numbers, spaces, dash(-), 5 - 20 characters" autocomplete = "off" value="{{ Input::old('branch_name') }}" required></td>
        </tr>
        <tr>
            <td style="padding:10px;"><label>Branch Code</label></td>
            <td valign="top" style="padding:10px;"><b>:</td>    
            <td><input type="text" class="form-control" name="branch_code" id="branch_code" pattern="[a-zA-Z0-9-]{6,8}" maxlength = "8" title="Accepts only combination of letters and numbers, no spaces, 6 - 8 characters" style="text-transform:uppercase;" onkeyup="javascript:this.value=this.value.toUpperCase();" autocomplete = "off" style="text-transform:uppercase" required></td>
        </tr>
        <tr>
            <td valign="top" style="padding:10px;"><label>Location</label></td>
            <td valign="top" style="padding:10px;"><label>:</label></td>
            <td><textarea class="form-control" name="branch_location" id="branch_location" pattern="[a-zA-Z0-9-. ]{5,200}" onkeyup="autocap(this.id)" maxlength = "200" title="Accepts only letters, numbers, spaces, dash(-), dot(.), 5 - 50 characters" autocomplete = "off" rows="5" required></textarea></td>
        </tr>
    </table>
</div>
<div class="modal-footer">
    <button class="btn btn-primary">Save</button>
    <button class="btn btn-default" data-dismiss="modal" onclick="clearAddBranch()">Close</button>
</div>
</div>
</div>
</div>
{{ Form::close() }}

控制器

public function postBranches() {
    $branch_code = DB::table('branches')->where('branch_code', Input::get('branch_code'))->first();
    if($branch_code) {
        return Redirect::to('main/branches')->with('error','<button type="button" class="close" data-dismiss="alert" style="margin:10px 5px 10px 5px;">&times;</button><h5 class="alert alert-danger"><span class="glyphicon glyphicon-exclamation-sign"></span><strong> Warning! </strong>Branch code already exists.</h5>');
    } else {
        $branch = new Branch;
        $branch->branch_name = Input::get('branch_name');
        $branch->branch_code = Input::get('branch_code');
        $branch->branch_location = Input::get('branch_location');
        $branch->branch_logo = "blank.png";
        $branch->status = "1";
        $branch->save();
        return Redirect::to('main/branches')->with('message', '<button type="button" class="close" data-dismiss="alert" style="margin:5px 10px;">&times;</button><h5 class="alert alert-info"><span class = "glyphicon glyphicon-ok-sign"></span><strong> Success! </strong>New Branch has been successfully added!</h5>');                     
    }
}

0 个答案:

没有答案