Question

i have a problem with the validation form. It does not work if i put "required", example:

controller:

public function updateBenefit(){
    $result = array();
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('beneficio', 'Nombre del Beneficio', 'required|alpha');
    $this->form_validation->set_rules('info', 'Info', 'alpha');
    $this->form_validation->set_rules('descrip', 'Descripción', 'alpha');
    $this->form_validation->set_rules('orden', 'Orden', 'integer');

    // $this->form_validation->set_rules('fecha', 'Fecha', 'date_valid');

    $this->form_validation->set_message('required', 'El campo %s es requerido');

    if ($this->form_validation->run() == TRUE){

        if (isset($_POST['id'])){

            $idb = $_POST['id'];
            $benefit = BeneficiosManager::getInstance()->getHome($idb);

            $result['message'] = "Se ha modificado el Beneficio con éxito"; 

        } else{
            $benefit = BeneficiosManager::getInstance()->create();

            $result['message'] = "Se ha cargado el Beneficio con éxito"; 
        }

        $benefit->nombre = ucfirst(strtolower($_POST['beneficio']));
        $benefit->content = ucfirst(strtolower($_POST['descrip']));
        $benefit->intro = ucfirst(strtolower($_POST['info']));
        $benefit->active = $_POST['optionsRadios2'];
        $benefit->orden = $_POST['orden'];

        // $benefit->date = $_POST['fecha'];

        BeneficiosManager::getInstance()->save($benefit);

        }else{
            //no se validaron los datos ingresados
            $result['message'] = "Error validación";
        }

        echo json_encode($result);
}

view:

    {extends file='admin/base/base.tpl'}

{block name='content'}

<h3>Cargar Beneficio </h3>
</br>
<form action="{site_url()}admin/updateBenefit" class="form-horizontal" method="post" id="" enctype="multipart/form-data">

   <div class="control-group">
      <label class="control-label">Beneficio</label>
      <div class="controls">
         <input type="text" name="beneficio" value="" class="m-wrap medium" />
         <span class="help-inline">Nombre del Beneficio</span>
      </div>
   </div>

   <div class="control-group">
      <label class="control-label">Info</label>
      <div class="controls">
         <textarea name="info" class="medium m-wrap" rows="3"></textarea>
         <span class="help-inline">Información Clave</span>
      </div>
   </div>

   <div class="control-group">
      <label class="control-label">Descripción</label>
      <div class="controls">
         <textarea name="descrip" class="large m-wrap" rows="3"></textarea>
         <span class="help-inline">Descripción del Beneficio</span>
      </div>
   </div>

   <div class="control-group">
      <label class="control-label">Activo</label>
      <div class="controls">
            <label class="radio line">
               <input type="radio" name="optionsRadios2" value="1"/>Si</input>
            </label>
            <label class="radio line">   
               <input type="radio" name="optionsRadios2" value="0"/>No</input>
            </label>
         <span class="help-inline">Ofrecer Beneficio</span>
      </div>
   </div>

   <div class="control-group">
      <label class="control-label">Orden</label>
      <div class="controls">
         <input type="text" name="orden" value="" class="m-wrap small" />
         <span class="help-inline">Prioridad del Beneficio</span>
      </div>
   </div>

   <div class="control-group">
      <label class="control-label">Fecha</label>
      <div class="controls">
         <input type="text" name="fecha" value="{$smarty.now|date_format}" class="m-wrap medium" />
         <span class="help-inline"></span>
      </div>
   </div>   

   <div class="form-actions">
      <button type="submit" class="btn blue"><i class="icon-ok"></i> Guardar</button>
      <button type="button" class="btn">Cancelar</button>
   </div>

</form>

{/block}

what might the problem be?

if i remove the "required" field, the form validates... but if i put it, it doesn't... i don't know what else to try, can't understand why this is happening

the code is working now, thanks to @Jonathan that corrected me, and i was also making an imput mistake when i was trying this method. I was trying to put two words in the same field (i.e: bon vivir); so the correct input would be: bonvivir.

Was it helpful?

Solution

I'm not sure you are using "title" as the name of your real title input element. Because I found you use this code to assign your title.

$benefit->title = ucfirst(strtolower(trim($_POST['beneficio'])));

So you may want to try to use

$this->form_validation->set_rules('beneficio', 'Nombre del Beneficio', 'required|alpha');

instead.

I am not sure I had the right guess. Just give it a go. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top