php - phalcon validate form fields without saving to database -


am in need validate form fields , manipulate them out saving database. have done

in controller

<?php use phalcon\mvc\model\criteria; use phalcon\paginator\adapter\model paginator; use phalcon\mvc\view;  class userscontroller extends controllerbase {      public function loginaction() {          if($this->request->ispost()) {              $user = new users();             $validates = $user->validation($this->request->getpost());               // validation works fine, cancelonfail in model doesn't seems work,               if($validates) {                 echo 'valid inputs';             }             else {                 print_r($user->getmessages());                 // how can show these error messages below corresponding input fields in view.                 // show error message follows, if field has more 1 validation conditions,                 // eg: username have notempty , valid e-mail validation set in model if username empty show not empty message,                 // if username not empty , if not valid e-mail , show not valid email message.             }             exit();         }     }  } ?>    

am trying validate model , looks follows

<?php use phalcon\mvc\model\validator; use phalcon\mvc\model\validator\presenceof; use phalcon\mvc\model\validator\email;  class users extends \phalcon\mvc\model {      public function validation() {          $this->validate(new presenceof(             array(                'field'  => 'username',                'message' => 'username required.',                'cancelonfail' => true             )         ));          $this->validate(new email(             array(                 'field'  => 'username',                 'message' => 'username must valid e-mail.'             )         ));          $this->validate(new presenceof(             array(                 'field'  => 'password',                 'message' => 'password required.'             )         ));          return $this->validationhasfailed() != true;     } } ?> 

my view file follows

 <?php      echo $this->tag->form(array("users/login", "role" => "form"));     echo $this->tag->textfield(array('username', 'class' => 'form-control', 'placeholder' => 'e-mail', 'type' => 'email', 'tabindex' => 1));      echo $this->tag->passwordfield(array('password', 'class' => 'form-control', 'placeholder' => 'password', 'type' => 'password', 'tabindex' => 2));      echo $this->tag->submitbutton(array('login','class' => 'btn btn-sm btn-success btn-block', 'tabindex' => 5));  ?> </form> 

how can achieve following,

1) check if form fields validates correctly given in model controller.

2) not looking save form data, validate it.

3) show corresponding error messages below input field in view.

thankz

you need create form, bind entity , validate on post request. see http://docs.phalconphp.com/en/latest/reference/forms.html#validation

edit: display error messages can in controller

// ... $messages = array(); foreach ($user->getmessages() $message) {     $messages[$message->getfield()] = $message->getmessage(); }  $this->view->messages = $messages; //... 

now have $messages in view.

i think should use form in case. trying validate user login in model, simple form validation. in model, validates business rules of users in app.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -