开发者

Sending Errors Back to Form: Code Igniter

I'm having trouble getting the form validation library to send my form errors back to my form in this case.

I have a controller that handles uploading images called addImage.php. This controller only handles the do_upload processing. I have a view called uploadimage.php that contains the upload form and submits to /addImage/do_upload.

The upload form is loaded on the front page of my website using a template in code igniter using

<?php $this->load->view('uploadimage'); ?>

The front page controller is contained in home.php

Right now after validation fails, I'm just redirecting to the homepage which clearly doesn't load the errors back (in addImage.开发者_如何学Gophp)

if($this->_submit_validate() == FALSE)
        {
                redirect('/', 'location');
                return;         
        }

How can I redirect to my template_front.php while keeping those errors. Can I somehow call my home.php controller from the uploadimage.php controller to do this? I've confused myself trying to explain it! If this is totally unclear, let me know and I'll try to clarify.


Per the Documentation, you are suppose to simply re-load the view file on failure.

  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('myform');
  }
  else
  {
   $this->load->view('formsuccess');
  }

a redirect generates a new server request which flushes the validation error information.


on validation failure you should reload the form. May be you want to add a button to concel uploading. On the view, you should add some tag to show errors (there are lots of info about validation helpares) like in:

<?=form_open_multipart("/personas/savefoto", array('class' => "form3") )?>
            <h3><?=$heading?></h3>

            <div class="center">
                <?php echo '<strong>'.mb_convert_case($record['nombre'].' '.$record['apellido1'].' '.$record['apellido2'], MB_CASE_TITLE).'</strong><br/>';
                if( file_exists("fotos/e".MATRIZ."/b".$record['id'].".jpg")){
                    ?>
                    <img class="foto" src="<?php echo base_url()."fotos/e".MATRIZ."/b".$record['id']?>.jpg"/>
                    <br/><br/>
                    <?php
                } ?>
            </div>

            <div class="form-row">
                <label for="imagen">Nueva imagen <br/>(jpg, gif o png)</label>
                <input type="file" name="userfile" size="20" />
                <br/>

                    <?php if(isset($error_image)) echo '<p class="error">'.$error_image.'</p>'; ?>
            </div>
            <div class="form-row center">
                <input type="submit" value="Aceptar" />
                <input type="button" value="Cancelar" onclick="location.href='/system.php/personas/admin';">
            </div>
            <?=form_close();?>

look for the if(isset($error_image))


You could utilize the validation_errors() function and set them to a session variable

$this->session->set_userdata(array('form_errors', validation_errors()));

then access them on your redirect page.

echo $this->session->userdata('form_errors');
$this->session->unset_userdata('form_errors'); // prevent them from being stored past use
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜