文件上载表单验证

时间:2014-08-20 20:05:24

标签: validation laravel-4

我正在使用Laravel 4.1,我正在尝试使用内置的Validator选项将上传文件的大小限制为2mb。

但是,当我使用超大文件(10mb)测试验证时,验证不会触发。相反,Input :: all()完全为空,导致发生其他错误。

对于使用文件进行表单验证,我有什么遗漏吗?

这是我正在使用的验证器

$validator = Validator::make(Input::all(), 
    array(
        'submission_status'      => 'required',
        'photo'                  => 'required_if:submission_status,Received|max:1200',
    ),
    $messages
);

模板代码 - 使用Twig作为模板引擎

{{ form_open({'url': request_url(), 'method': 'post', 'role': 'form', 'name': 'photo_form', 'id': 'photo-form', 'enctype': 'multipart/form-data'})|raw }}
    <div class="row">
        <div class="col-md-12">
            Please submit a photo of just yourself. A head-and-shoulders photo works well.
        </div>
    </div>
    <div class="row">
        <div class="col-md-1">&nbsp;</div>
        <div class="col-md-11">
            {{ form_radio('submission_status', 'Received', false, {'id': 'photo-now', 'required': 'required', 'aria-describedby': 'submission-status-error'})|raw }} Upload photo now (The file should be no larger than 1.2 megabytes, and in jpeg format)<br />
            {{ form_radio('submission_status', 'Photo by Web promised', false, {'id': 'photo-later', 'required': 'required', 'aria-describedby': 'submission-status-error'})|raw }} Upload photo later<br />
            {{ form_radio('submission_status', 'No photo desired', false, {'id': 'no-photo', 'required': 'required', 'aria-describedby': 'submission-status-error'})|raw }} No photo desired<br />
            <span class="error" id="submission-status-error"></span>
        </div>
    </div>

    <div id="photo-now-form">
        <div>&nbsp;</div>
        <div class="row">
            <div class="col-md-12">
                <p>Please select a photograph of <b>just</b> yourself in jpeg format.<br />
                    <ul>
                        <li>Filenames must have a ".jpg" extension (e.g., myphoto.jpg).</li>
                        <li>The image should be about 4 x 5 inches and have a resolution of 200 pixels per inch.</li>
                        <li>The jpeg file should be between 500 and 1,200 kilobytes (0.5 to 1.2 MB).</li>
                    </ul>
                </p>
            </div>
        </div>
        <div>&nbsp;</div>

        <div class="row">
            <div class="form-group col-md-6">
                {{ form_file('photo', {'class': 'form-control', 'id': 'photo', 'required': 'required'})|raw }}
            </div>
        </div> <!-- row -->
    </div>
    <hr />

    <div class="row">
        <div class="form-group col-md-4 col-md-offset-4">
            {{ form_hidden('_id', candidate._id, {'class': 'form-control', 'id': '_id'})|raw }}
            {{ form_hidden("candidate_id", candidate.candidate_id, {'class': 'form-control', 'id': 'candidate-id'})|raw }}

            {{ form_hidden('page', page, {'class': 'form-control', 'id': 'page'})|raw }}
            {{ form_submit('Submit', {'id': 'save-questionnaire', 'class': 'form-control btn btn-primary'})|raw }}
        </div>
    </div>
{{ form_close()|raw }}

谢谢! --Lisa

1 个答案:

答案 0 :(得分:0)

这最终成为表单的问题,而PHP处理文件上传的PHP设置则更多。我最终没有提出解决问题的方法,因为在php.ini文件中删除对上传文件大小的限制是不可行的。

由于表单使用仅限于非常有限的一组用户,因此我暂时在这个问题上添加了一个引脚。