Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validate without form

I want to validate when user submit.

How to validate without the form.

My class validate such as:

namespace Glang\Validate;

use Phalcon\Validation\Validator\PresenceOf;

class News{

private $_error = array();

function __construct(){

  $validation = new PresenceOf();
  if($validation->isValid($_POST['title'])):
      $this->_error['title']    = 'Not empty';
  endif;
}

function getMessage(){
  return $this->_error;
}

}

I know isValid isn't exist. How to fix this. Thanks.



33.0k

Hey man

You can extend class Validate Phalcon any class of you, see here http://docs.phalconphp.com/en/latest/reference/validation.html#validation-messages

Hey man

You can extend class Validate Phalcon any class of you, see here http://docs.phalconphp.com/en/latest/reference/validation.html#validation-messages

My class validate extends:

News extends \Phalcon\Validation

if use form it look like:

$this->add('name', new PresenceOf(array( 'message' => 'The name is required' )));

Now, how to use Phalcon\Validation\Validator\PresenceOf with no form.

I don't know what params to input PresenceOf(?) and how to check it is isvalid

Thanks.



1.4k
edited Jan '15

If you want to write your own validator

 $ele = new Element/TextArea("json_submit");
 $ele->addValidator(new JsonValidator(["message" => "contain valid json"]));
 class  JsonValidator extends Validator implements ValidatorInterface
{
    function validate($validator, $field)
    {
        return json_decode($validator->getValue($field));
    }
}

If you plan to use it in a model.

class MyModel extends \Model
{
    public function validation()
    {
        $this->validate->(new JsonValidator(["field" => "myField", "message" => "contain valid json"]));
    }
}

if you want to use it as a combined validation

class MyValidation extends Validation
{
  public function initialize()
  {
         $this->add('name', new PresenceOf(array(
             'message' => 'The name is required'
         )));

         $this->add('email', new PresenceOf(array(
             'message' => 'The e-mail is required'
         )));

         $this->add('email', new Email(array(
             'message' => 'The e-mail is not valid'
         )));
     }
 }
 $validation = new MyValidation();
 $validation->validate($_POST);


1.4k

i swear the mark down is hard to use :O



48.2k
Accepted
answer

You can add validation to the Model, without using Form classes. In fact, I never use the Form classes.

The Model validation documentation is here: http://docs.phalconphp.com/en/latest/reference/models.html#validating-data-integrity

You can add validation to the Model, without using Form classes. In fact, I never use the Form classes.

The Model validation documentation is here: http://docs.phalconphp.com/en/latest/reference/models.html#validating-data-integrity

Exactly i need.

i mistake of the thinking about the validation in phalcon. I think it same in zend. Thank you so much !