Skip to content

Commit

Permalink
Merge pull request phalcon#2213 from dreamsxin/nfr_validate
Browse files Browse the repository at this point in the history
Add `allowEmpty` option to  \Phalcon\Mvc\Model\Validator\*
  • Loading branch information
Phalcon committed Mar 21, 2014
2 parents 80d24d6 + bde1bbc commit bbbfd7a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 7 deletions.
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/email.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Email){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Email, validate){

zval *record, *option = NULL, *field_name = NULL, *regs, *invalid = NULL;
zval *record, *option = NULL, *field_name = NULL, *allow_empty = NULL, *regs, *invalid = NULL;
zval *value = NULL, *pattern, *match_pattern, *match_zero;
zval *message = NULL, *type, *is_set_code = NULL, *code = NULL;

Expand All @@ -111,6 +111,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Email, validate){

PHALCON_CALL_METHOD(&value, record, "readattribute", field_name);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

/**
* We check if the email has a valid format using a regular expression
*/
Expand Down
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/exclusionin.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Exclusionin){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Exclusionin, validate){

zval *record, *option = NULL, *field_name = NULL, *is_set = NULL, *domain = NULL;
zval *record, *option = NULL, *field_name = NULL, *allow_empty = NULL, *is_set = NULL, *domain = NULL;
zval *value = NULL, *message = NULL, *joined_domain, *type, *is_set_code = NULL, *code = NULL;

PHALCON_MM_GROW();
Expand Down Expand Up @@ -126,6 +126,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Exclusionin, validate){

PHALCON_CALL_METHOD(&value, record, "readattribute", field_name);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

/**
* We check if the value contained in the array using "in_array" from the PHP
* userland
Expand Down
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/inclusionin.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Inclusionin){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Inclusionin, validate){

zval *record, *field = NULL, *field_name = NULL, *is_set = NULL, *domain = NULL;
zval *record, *field = NULL, *field_name = NULL, *allow_empty = NULL, *is_set = NULL, *domain = NULL;
zval *value = NULL, *option, *message = NULL, *joined_domain, *is_set_code = NULL, *code = NULL;
zval *type;

Expand Down Expand Up @@ -127,6 +127,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Inclusionin, validate){

PHALCON_CALL_METHOD(&value, record, "readattribute", field_name);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

/**
* Check if the value is contained in the array
*/
Expand Down
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/numericality.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Numericality){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Numericality, validate){

zval *record, *option = NULL, *field = NULL, *value = NULL, *message = NULL;
zval *record, *option = NULL, *field = NULL, *allow_empty = NULL, *value = NULL, *message = NULL;
zval *type, *is_set_code = NULL, *code = NULL;

PHALCON_MM_GROW();
Expand All @@ -103,6 +103,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Numericality, validate){

PHALCON_CALL_METHOD(&value, record, "readattribute", field);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

/**
* Check if the value is numeric using is_numeric in the PHP userland
*/
Expand Down
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Regex){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Regex, validate){

zval *record, *option = NULL, *field_name = NULL, *is_set = NULL, *value = NULL;
zval *record, *option = NULL, *field_name = NULL, *allow_empty = NULL, *is_set = NULL, *value = NULL;
zval *failed = NULL, *matches, *pattern = NULL, *match_pattern;
zval *match_zero, *message = NULL, *type, *is_set_code = NULL, *code = NULL;

Expand Down Expand Up @@ -119,6 +119,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Regex, validate){

PHALCON_CALL_METHOD(&value, record, "readattribute", field_name);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

PHALCON_INIT_VAR(failed);
ZVAL_BOOL(failed, 0);

Expand Down
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/stringlength.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_StringLength){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_StringLength, validate){

zval *record, *option = NULL, *field = NULL, *is_set_min = NULL, *is_set_max = NULL;
zval *record, *option = NULL, *field = NULL, *allow_empty = NULL, *is_set_min = NULL, *is_set_max = NULL;
zval *value = NULL, *length = NULL, *invalid_maximum = NULL, *invalid_minimum = NULL;
zval *maximum = NULL, *message = NULL, *type = NULL, *minimum = NULL, *is_set_code = NULL, *code = NULL;

Expand Down Expand Up @@ -128,6 +128,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_StringLength, validate){
}

PHALCON_CALL_METHOD(&value, record, "readattribute", field);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

/**
* Check if mbstring is available to calculate the correct length
Expand Down
16 changes: 15 additions & 1 deletion ext/mvc/model/validator/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Url){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Url, validate){

zval *record, *option = NULL, *field = NULL, *value = NULL, *flag, *is_valid = NULL;
zval *record, *option = NULL, *field = NULL, *allow_empty = NULL, *value = NULL, *flag, *is_valid = NULL;
zval *message = NULL, *type, *is_set_code = NULL, *code;

PHALCON_MM_GROW();
Expand All @@ -102,6 +102,20 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Url, validate){

PHALCON_CALL_METHOD(&value, record, "readattribute", field);

/*
* Allow empty
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "allowEmpty", 1);

PHALCON_CALL_METHOD(&allow_empty, this_ptr, "issetoption", option);

if (allow_empty && zend_is_true(allow_empty)) {
if (PHALCON_IS_EMPTY(value)) {
RETURN_MM_TRUE;
}
}

PHALCON_INIT_VAR(flag);
ZVAL_LONG(flag, 273);

Expand Down

0 comments on commit bbbfd7a

Please sign in to comment.