I have this situation. For example,
Path: localhost/intranet/
If i enter this url:
localhost/intranet/usuarios/actualizar/richard
and leave all fields blank and send submit, the validation runs fine and show me the error: “all those fields are required”. But, if i click send again, it will send me to:
localhost/intranet/usuarios/actualizar/
Hint: usuarios = USERS in english, actualizar = UPDATE_INFO.
So, as you can see, the argument, in this case richard is mising. Im running 2.1 CodeIgniter. Why is this happening? i don’t want CodeIgniter miss the argument because it will show many errors related to CI not being able to find a argument to work with.
Is this normal? Can anyone can click two times when validation fails to see if is the same behavior?
UPDATE #1 Added method for update the data
public function actualizar() {
// we check if the form was submited. If not, load register view.
if($_SERVER['REQUEST_METHOD'] == "POST") {
// translation for errors.
$required_msg = "<strong>%s</strong> es un campo obligatorio.";
$exact_length_msg = "<strong>%s</strong> debe contener 10 dígitos exactamente.";
$numeric_msg = "<strong>%s</strong> debe contener solamente dígitos.";
$email_msg = "<strong>%s</strong> debe ser una dirección válida.";
$unique_msg = "<strong>%s</strong> ya está registrado en el sistema. Seleccione uno distinto.";
// set the messages with translations.
$this->form_validation->set_message('required', $required_msg);
$this->form_validation->set_message('exact_length', $exact_length_msg);
$this->form_validation->set_message('numeric', $numeric_msg);
$this->form_validation->set_message('valid_email', $email_msg);
$this->form_validation->set_message('is_unique', $unique_msg);
// set the rules for each input field
$this->form_validation->set_rules('userName', 'Nombre', 'trim|required');
$this->form_validation->set_rules('userLastName', 'Apellido', 'trim|required');
$this->form_validation->set_rules('userUsername', 'Nombre de Usuario', 'trim|required|is_unique[usuarios.userUsername]');
$this->form_validation->set_rules('userCellphone', 'Teléfono', 'trim|numeric|exact_length[10]');
$this->form_validation->set_rules('userEmail', 'Correo Electrónico', 'trim|required|valid_email||is_unique[usuarios.userEmail]');
$this->form_validation->set_rules('userAddress', 'Dirección', 'trim|required');
// set the custom delimiters for each error msg
$this->form_validation->set_error_delimiters('<li>', '</li>');
// we check if the validation pass the rules. If not, show the view again with errors msgs.
if($this->form_validation->run() == FALSE) {
$data['title'] = "Registrar Usuario";
$data['page_title'] = "Panel de Control";
$data['header'] = "header";
$data['scripts'] = array('alert-dismiss');
$data['body'] = "usuarios-actualizar";
$this->load->view('template', $data);
} else {
// if validation went ok, we capture the form data.
$fields = array(
'userName' => $this->input->post('userName'),
'userLastName' => $this->input->post('userLastName'),
'userUsername' => $this->input->post('userUsername'),
'userEmail' => $this->input->post('userEmail'),
'userAddress' => $this->input->post('userAddress'),
'userCellphone' => $this->input->post('userCellphone'),
'userIn' => $this->input->post('userIn'),
'userOut' => $this->input->post('userOut'),
'userLastLogin' => '',
'userRole' => $this->input->post('userRole')
);
$userName = $this->input->post('userName');
$userLastName = $this->input->post('userLastName');
$userEmail = $this->input->post('userEmail');
$userID = $this->input->post('userID');
$userUsername = $this->input->post('userUsername');
// send the $data to the model
if($this->Data_model->update_entry($userID, $fields) == TRUE) {
$this->session->set_flashdata('alert', 'El perfil ha sido actualizado con éxito.');
$this->session->set_flashdata('style', 'success');
redirect('usuarios/perfil/' . $userUsername);
} else {
$this->session->set_flashdata('alert', 'Hubo un error actualizando el perfil. Inténtelo de nuevo.');
$this->session->set_flashdata('style', 'error');
redirect('usuarios/perfil/' . $userUsername);
}
}
// If the request method is not POST, it means that form was not submitted.
} else {
$data['title'] = "Actualizar Datos";
$data['page_title'] = "Actualizar Datos";
$data['header'] = "header";
$data['scripts'] = array('alert-dismiss');
$data['body'] = "usuarios-actualizar";
$data['user'] = $this->Data_model->get_profile_for_update($userUsername);
$this->load->view('template', $data);
}
}
<?=form_close?>
I just told CI load the view, not redirect and lose the url. I repit: first time, it works cool. But if i don’t change anything and just press SEND again, it fails 🙁 Just noticed that tonight. Just by curiosity pressed send again and it went to hell.
Hope someone can give me some idea of whats going on. Thanks.
P.S. Maybe this guy has the same problem. Not quite sure…
CodeIgniter Set Form Validation Redirect URL
UPDATE #2 Added view code
<?=form_open('usuarios/actualizar')?>
<?php
$config = array(
'type' => 'submit',
'content' => '<i class="icon-ok icon-white"></i> Guardar Usuario',
'class' => 'btn btn-primary'
);
?>
<div class="row">
<h2 class="section-title span10">Actualizar Usuario</h2>
<div class="span2">
<?=form_button($config)?>
</div>
</div>
<?php if (validation_errors() != null) : ?>
<div class="alert alert-warning fade in">
<a class="close" data-dismiss="alert">×</a>
<ul class="unstyled">
<?=validation_errors()?>
</ul>
</div>
<?php endif;?>
<div class="span5">
<h3 class="box-title">Datos Personales</h3>
<input type="hidden" name="userID" value="<?=$user->userID ?>">
<label>Nombre<span class="required">*</span></label>
<input type="text" class="span4" name="userName" value="<?=set_value('username', $user->userName)?>">
<label>Apellido<span class="required">*</span></label>
<input type="text" class="span4" name="userLastName" value="<?=set_value('userLastName', $user->userLastName)?>">
<label>Teléfono</label>
<input type="text" class="span4" name="userCellphone" value="<?=set_value('userCellphone', $user->userCellphone)?>">
<label>Correo Electrónico<span class="required">*</span></label>
<input type="text" class="span4" name="userEmail" value="<?=set_value('userEmail', $user->userEmail)?>">
<label>Dirección<span class="required">*</span></label>
<textarea class="span4" name="userAddress"><?=set_value('userAddress', $user->userAddress)?></textarea>
</div>
It doesnt work because it can’t find the argument so the $user object can’t be created.
That lead like a ton of errors.
SOLUTION:
I don’t have so much rep so i can’t answers myself but here is the solution:
Oh my god. Maybe im falling asleep.
As Madmartigan states, i wasn’t using the “argument”. So, i added this:
$data['user'] = $this->Data_model->get_profile_for_update($this->uri->segment(3));
Sooo, i doesn’t matter if it “lose” the argument in the URL, it works because before it loads the view, call the $data[‘user’] stuff 🙂
Thank you for your help guys, you are the best.
It seems pretty obvious:
This will always post to
usuarios/actualizar, so you would lose any other URI segments or query string. Just leave it blank to post to the current url:…although, I don’t see that
actualizar()actually does anything with the arguments passed to it in the url, so it’s not clear why you want it to beusuarios/actualizar/richard, asricharddoesn’t appear to be used.EDIT: OK, now I see it:
$this->uri->segment(3). You need to be able to handle the case of this not being set, so in addition do this in your controller to prevent errors:Just for the record, you can also do this to set a default “username”:
Because CI is pretty strict about what it allows in the URL, you might be better off using the query string instead for names if you expect characters that aren’t allowed in your
$config['permitted_uri_chars'].