codeigniter验证错误,字段为“required”

时间:2013-09-05 14:37:28

标签: php codeigniter validation field required

我的验证表格有问题。如果我把“必需”,例如:

,它不起作用

控制器:

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

视图:

    {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}

问题可能是什么?

如果我删除“必填”字段,表单验证...但如果我把它,它不...我不知道还有什么尝试,不明白为什么会发生这种情况< / p>

代码现在正在运行,感谢@Jonathan纠正了我,当我尝试这种方法时,我也犯了一个错误的错误。我试图在同一个领域放两个字(即:bon vivir);所以正确的输入是:bonvivir。

1 个答案:

答案 0 :(得分:0)

我不确定您是否使用“title”作为真实标题输入元素的名称。因为我发现你使用这段代码来分配你的头衔。

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

所以你可能想尝试使用

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

代替。

我不确定我是否有正确的猜测。试一试吧。希望这会有所帮助。