We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



58.4k

Hey man

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

Hey man

You can extend class Validate Phalcon any class of you, see here https://docs.phalcon.io/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.



2.1k
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);


2.1k

i swear the mark down is hard to use :O



125.8k
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: https://docs.phalcon.io/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: https://docs.phalcon.io/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 !