不返回成功的表单验证规则

时间:2012-04-21 17:34:07

标签: php codeigniter

我正在试图找出为什么我的表单验证运行函数返回false。根据文档说明如下:

  

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' );   
                }
            }
        });
    }
});

1 个答案:

答案 0 :(得分:1)

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

您可以使用CodeIgniter的URL帮助程序并使用它:action="<?php echo site_url("your_controller_name/biographySubmit"); ?>"

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