0

我试图弄清楚为什么我的表单验证运行函数返回错误。根据文档,它说以下内容:

run() 函数仅在成功应用您的规则且没有任何失败的情况下返回 TRUE。

这是我的规则的样子:

$this->form_validation->set_rules('nicknames', 'Nicknames',
        'required');
    $this->form_validation->set_rules('hometown', 'Hometown',
        'required');
    $this->form_validation->set_rules('height', 'Height',
        'required');
    $this->form_validation->set_rules('Weight', 'Weight',
        'required');
    $this->form_validation->set_rules('manager', 'Manager',
        'required|integer');   
    $this->form_validation->set_rules('setup', 'Setup',
        'required');
    $this->form_validation->set_rules('finisher', 'Finisher',
        'required');
    $this->form_validation->set_rules('biography', 'Biography',
        'required');   

这是帖子提交运行的内容:

biography   Kid coming out of college getting the chance of a lifetime.
finisher    Idolizer
height  5'4"
hometown    Las Vegas, Nevada
manager 0
nicknames   "The Quintessential Cruiserweight of KOW!"; "K-Dub"
setup   That's Wondeful
submit  Submit
weight  140 lbs.

http://pastebin.com/F9XeEibf

function biographySubmit()
{
    $outputArray = array('error' => 'yes', 'message' => 'unproccessed');
    $outputMsg = '';
    // Sets validation rules for the login form
    $this->form_validation->set_rules('nicknames', 'Nicknames',
        'required');
    $this->form_validation->set_rules('hometown', 'Hometown',
        'required');
    $this->form_validation->set_rules('height', 'Height',
        'required');
    $this->form_validation->set_rules('Weight', 'Weight',
        'required');
    $this->form_validation->set_rules('manager', 'Manager',
        'required|integer');   
    $this->form_validation->set_rules('setup', 'Setup',
        'required');
    $this->form_validation->set_rules('finisher', 'Finisher',
        'required');
    $this->form_validation->set_rules('biography', 'Biography',
        'required');   

    // Checks to see if login form was submitted properly(
    if (!$this->form_validation->run())
    {
        $outputArray['message'] =
            'There was a problem submitting the form! Please refresh the window and try again!';
    }
    else
    {
        $bioFields = array($this->input->post('nicknames'), $this->input->post('hometown'), $this->input->post('height'), $this->input->post('weight'), $this->input->post('manager'), $this->input->post('setup'), $this->input->post('finisher'), $this->input->post('biography'));          
        if ($this->bios->updateBiography($bioFields, $this->session->userdata('defaultRosterListID')))
        {
            $outputArray = array('success' => 'Yes', 'message' =>
                                        'Biography were updated successfully!');
        }
        else
        {
            $outputArray['message'] =
            'The biopgraphy was not able to be updated in the database. Please try again later!';
        }
    }
    echo json_encode($outputArray);
}

jQuery代码:

var validateform = $("#biographyForm").validate({
    invalidHandler: function(form, validator) {
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1
            ? 'You missed 1 field. It has been highlighted.'
            : 'You missed ' + errors + ' fields. They have been highlighted.';
            $('.box #biographyForm .content').removeAlertBoxes();
            $('.box #biographyForm .content').alertBox(message, {type: 'warning', icon: true, noMargin: false});
            $('.box #biographyForm .content .alert').css({
                margin: '0',
                borderLeft: 'none',
                borderRight: 'none',
                borderRadius: 0
            }).delay( 3000 ).fadeOut( 'slow' );
        } else {
            $('.box #biographyForm .content').removeAlertBoxes();
        }
    },
    ignore : 'input:hidden:not(:checkbox):not(:radio)',
    showErrors : function(errorMap, errorList) {
                    this.defaultShowErrors();
                    var self = this;
                    $.each(errorList, function() {
                        var $input = $(this.element);
                        var $label = $input.parent().find('label.error').hide();
                        if (!$label.length) {
                            $label = $input.parent().parent().find('label.error');
                        }
                        if($input.is(':not(:checkbox):not(:radio):not(select):not([type=file])')) {
                            $label.addClass('red');
                            $label.css('width', '');
                            $input.trigger('labeled');
                        }
                        $label.fadeIn();
                    });
                },
    errorPlacement : function(error, element) {
                    if(element.is(':not(:checkbox):not(:radio):not(select):not([type=file])')) {
                        error.insertAfter(element);
                    } else if(element.is('select')) {
                        error.appendTo(element.parent());
                    } else if (element.is('[type=file]')){
                        error.insertAfter(element.parent());
                    } else {
                        error.appendTo(element.parent().parent());
                    }

                    if ($.browser.msie) {
                        error.wrap('<div class="error-wrap" />');
                    }
                },
    submitHandler: function(form) {
        var dataString = $('#biographyForm').serialize();
        $.ajax({
            type: 'POST',
            url: 'biography/biographySubmit',
            data: dataString,
            dataType: 'json',
            success:  function(data) {
                if (data.error) {
                    $('.box #biographyForm .content').removeAlertBoxes();
                    $('.box #biographyForm .content').alertBox(data.message, {type: 'warning', icon: true, noMargin: false});
                    $('.box #biographyForm .content .alert').css({
                        width: '',
                        margin: '0',
                        borderLeft: 'none',
                        borderRight: 'none',
                        borderRadius: 0
                    }).delay( 3000 ).fadeOut( 'slow' );
                }
                else
                {
                    $('.box #biographyForm .content').removeAlertBoxes();
                    $('.box #biographyForm .content').alertBox(data.message, {type: 'success', icon: true, noMargin: false});
                    $('.box #biographyForm .content .alert').css({
                        width: '',
                        margin: '0',
                        borderLeft: 'none',
                        borderRight: 'none',
                        borderRadius: 0
                    }).delay( 3000 ).fadeOut( 'slow' );   
                }
            }
        });
    }
});
4

1 回答 1

1

我不确定这是否会完全解决您的问题,但您的表单似乎没有action. 它应该是类似的东西action="[your site url]/your_controller_name/biographySubmit"

你可以使用 CodeIgniter 的 URL Helper 并使用它:action="<?php echo site_url("your_controller_name/biographySubmit"); ?>"

如果没有操作,表单中的数据将不会传递给biographySubmit控制器​​中的函数。

于 2012-04-21T22:57:58.163 回答