如何在Phalcon中验证表单中的日期?

时间:2016-10-25 18:51:12

标签: date datepicker mariadb phalcon volt

我使用Phalcon框架构建了一个网页。我有一个表格要求提供文本,整数和日期。我正在使用datepicker(https://jqueryui.com/datepicker/)来获取日期。当我在数据库中插入时,不插入此日期,只插入文本和int。 我认为这是日期格式的问题,所以我想验证它。 我可以验证文本字段中是否存在数据但我没有找到任何“验证器”来验证它是否为日期。

这是我的表单类:

<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Date;
use Phalcon\Forms\Element\Password;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;

class NuevakeywordForm extends Form{

public function initialize($entity = null, $options = null){
    // Trackeable
    $trackeable = new Text('trackeable');
    $trackeable->setLabel('Palabra Clave');
    $trackeable->setFilters(array('striptags', 'string'));
    $trackeable->addValidators(array(
        new PresenceOf(array(
            'message' => 'Palabra clave requerida'
            ))
        ));
    $this->add($trackeable);

    // IdCliente
    $idcliente = new Text('idcliente');
    $idcliente->setLabel('idcliente');
    $idcliente->addValidators(array(
        new PresenceOf(array(
            'message' => 'IdCliente is required'
            ))
        ));
    $this->add($idcliente);

    // Fecha Límite
    $fecha = new Text('fecha_final');
    $fecha->setLabel('Fecha Final');
    $fecha->addValidators(array(
        new PresenceOf(array(
            'message' => 'Por favor pon una fecha límite'
            ))
        ));
    $this->add($fecha);
    }
}

以下是观点:

<div class="page-header">
<h2>Registro de Nueva Keyword</h2>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<section class="container animated fadeInUp">
<div class="row">
    <div class="col-md-6 col-md-offset-3">
    <div id="login-wrapper">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">     
                    Registrar Nueva Keyword
                </h3>  
            </div>
            <div class="panel-body">
                {{ form('admin/nuevakeyword/', 'id': 'nuevakeywordForm', 'onbeforesubmit': 'return false') }}

                <fieldset>

                    <div class="control-group">
                        {{ form.label('trackeable', ['class': 'control-label']) }}
                        <div class="controls">
                            {{ form.render('trackeable', ['class': 'form-control']) }}
                        </div>
                    </div>

                    <div class="control-group">
                        {{ form.label('idcliente', ['class': 'control-label']) }}
                        <div class="controls">
                            {{ form.render('idcliente', ['class': 'form-control']) }}                           
                        </div>
                    </div>

                    <div class="control-group">
                        {{ form.label('fecha_final', ['class': 'control-label']) }}
                        <div id="datepicker" class="controls">
                            {{ form.render('fecha_final', ['class': 'form-control']) }}
                        </div>
                    </div>

                    <div class="form-actions">
                        {{ submit_button('Insertar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
                    </div>

                </fieldset>
                </form>
            </div>
        </div>
    </div>
    </div>
</div>
</section>

<script type="text/javascript">
    $(function() {
        $("#fecha_final").datepicker();
    });
</script>

这是控制器:

public function nuevakeywordAction(){
    $auth = $this->session->get('auth');
    $permiso = $auth['active'];
    if($permiso!='A'){return $this->forward('servicios/index');}

    $form = new NuevakeywordForm;

    if ($this->request->isPost()) {

        $trackeable = $this->request->getPost('trackeable', array('string', 'striptags'));
        $idcliente = $this->request->getPost('idcliente');
        $fecha = $this->request->getPost('fecha');

        $keyword = new Keyword();
        $keyword->trackeable = $trackeable;
        $keyword->cliente_idcliente = $idcliente;
        $keyword->fecha_inicial = new Phalcon\Db\RawValue('now()');
        $keyword->fecha_final = $fecha;
        if ($keyword->save() == false) {
            foreach ($keyword->getMessages() as $message) {
                $this->flash->error((string) $message);
            }
        } else {
            $this->tag->setDefault('trackeable', '');
            $this->tag->setDefault('idcliente', '');
            $this->tag->setDefault('fecha', '');
            $this->flash->success('Keyword agregada correctamente');
            return $this->forward('admin/verkeywords');
        }
    }

    $this->view->form = $form;
}

我正在使用MariaDB(与MySQL相同)。我正在尝试为日期找到“addValidator”,但也许是其他问题。任何帮助都会受到欢迎。

1 个答案:

答案 0 :(得分:2)