TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Combining $_POST and $_FILES Validation
  • I'm trying to validate both $_POST and $_FILES data and I have the following problem. Upload:: validation is working as expected. However $_POST variable validation seems to have no effect at all.

    With the code below all of the building_file validation works but the building_id validation doesn't work. I've used the same rule for building_id many places in my code and it works just fine. Is there something special that I need to do with file upload POST requests for validating values in the $_POST array?

    $post = Validate::factory(array_merge($_POST, $_FILES));

    $post->rule('building_id', 'numeric');

    $post->rule('building_file', 'Upload::not_empty', array());
    $post->rule('building_file', 'Upload::valid', array());
    $post->rule('building_file', 'Upload::type', array(array('xls', 'xlsx')));
    $post->rule('building_file', 'Upload::size', array('1M'));

    $view = View::factory('saveresponseplainview');

    $response_str = '';

    if ($post->check())
    {
    //ok
    }
    else
    {
    //error
    }

  • Do this instead:

    $post = Validate::factory(array_merge($_POST, $_FILES))
    ->rule('building_id', 'numeric')
    ->rules('building_file', array(
    'Upload::not_empty' => NULL,
    'Upload::valid' => NULL,
    'Upload::type' => array('xls', 'xlsx'),
    'Upload::size' => array('1M')
    );

    Then be sure that your form element opens with the multipart/form-data, if this isn't present then you are not going to have anything in the $_FILES array:

    <form action="/upload/save" enctype="multipart/form-data" method="post">
  • awellis,

    Thanks for the response. I've already validated that I have the file in $_FILES. I had originally formatted my validation code as you have it. I ended up pulling it apart into separate statements to see if I could figure out what was going on.

    Strange thing is that I haven't changed anything and the validation is now working. Not sure why. If I figure out what I changed I'll post a follow up here in case anyone else runs into the same issue.