Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFR #1171 Adding code support validation messages #1178

Merged
merged 9 commits into from
Sep 1, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
NFR #1171 Adding code support for Phalcon/Validation/Message
  • Loading branch information
Viktoras Bezaras committed Aug 30, 2013
commit b59fd6ea2593d00dd41624181fab5b37e5b4cb82
45 changes: 41 additions & 4 deletions ext/validation/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ PHALCON_INIT_CLASS(Phalcon_Validation_Message){
zend_declare_property_null(phalcon_validation_message_ce, SL("_type"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_validation_message_ce, SL("_message"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_validation_message_ce, SL("_field"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_validation_message_ce, SL("_code"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);

return SUCCESS;
}
Expand All @@ -66,11 +67,11 @@ PHALCON_INIT_CLASS(Phalcon_Validation_Message){
*/
PHP_METHOD(Phalcon_Validation_Message, __construct){

zval *message, *field = NULL, *type = NULL;
zval *message, *field = NULL, *type = NULL, *code = NULL;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 2, &message, &field, &type);
phalcon_fetch_params(1, 1, 3, &message, &field, &type, &code);

if (!field) {
PHALCON_INIT_VAR(field);
Expand All @@ -79,10 +80,16 @@ PHP_METHOD(Phalcon_Validation_Message, __construct){
if (!type) {
PHALCON_INIT_VAR(type);
}

if (!code) {
PHALCON_INIT_VAR(code);
ZVAL_LONG(code, 0);
}

phalcon_update_property_this(this_ptr, SL("_message"), message TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_field"), field TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_code"), code TSRMLS_CC);

PHALCON_MM_RESTORE();
}
Expand Down Expand Up @@ -114,6 +121,32 @@ PHP_METHOD(Phalcon_Validation_Message, getType){
RETURN_MEMBER(this_ptr, "_type");
}

/**
* Sets message code
*
* @param string $code
* @return Phalcon\Mvc\Model\Message
*/
PHP_METHOD(Phalcon_Validation_Message, setCode){

zval *code;

phalcon_fetch_params(0, 1, 0, &code);

phalcon_update_property_this(this_ptr, SL("_code"), code TSRMLS_CC);
RETURN_THISW();
}

/**
* Returns message code
*
* @return string
*/
PHP_METHOD(Phalcon_Validation_Message, getCode){

RETURN_MEMBER(this_ptr, "_code");
}

/**
* Sets verbose message
*
Expand Down Expand Up @@ -187,7 +220,7 @@ PHP_METHOD(Phalcon_Validation_Message, __toString){
*/
PHP_METHOD(Phalcon_Validation_Message, __set_state){

zval *message, *message_text, *field, *type;
zval *message, *message_text, *field, *type, *code;

PHALCON_MM_GROW();

Expand All @@ -201,8 +234,12 @@ PHP_METHOD(Phalcon_Validation_Message, __set_state){

PHALCON_OBS_VAR(type);
phalcon_array_fetch_string(&type, message, SL("_type"), PH_NOISY);

PHALCON_OBS_VAR(code);
phalcon_array_fetch_string(&code, message, SL("_code"), PH_NOISY);

object_init_ex(return_value, phalcon_validation_message_ce);
phalcon_call_method_p3_noret(return_value, "__construct", message_text, field, type);
phalcon_call_method_p4_noret(return_value, "__construct", message_text, field, type, code);

RETURN_MM();
}
Expand Down
9 changes: 9 additions & 0 deletions ext/validation/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ PHALCON_INIT_CLASS(Phalcon_Validation_Message);
PHP_METHOD(Phalcon_Validation_Message, __construct);
PHP_METHOD(Phalcon_Validation_Message, setType);
PHP_METHOD(Phalcon_Validation_Message, getType);
PHP_METHOD(Phalcon_Validation_Message, setCode);
PHP_METHOD(Phalcon_Validation_Message, getCode);
PHP_METHOD(Phalcon_Validation_Message, setMessage);
PHP_METHOD(Phalcon_Validation_Message, getMessage);
PHP_METHOD(Phalcon_Validation_Message, setField);
Expand All @@ -35,6 +37,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_validation_message___construct, 0, 0, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_INFO(0, field)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, code)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_validation_message_settype, 0, 0, 1)
Expand All @@ -49,6 +52,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_validation_message_setfield, 0, 0, 1)
ZEND_ARG_INFO(0, field)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_validation_message_setcode, 0, 0, 1)
ZEND_ARG_INFO(0, code)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_validation_message___set_state, 0, 0, 1)
ZEND_ARG_INFO(0, message)
ZEND_END_ARG_INFO()
Expand All @@ -57,6 +64,8 @@ PHALCON_INIT_FUNCS(phalcon_validation_message_method_entry){
PHP_ME(Phalcon_Validation_Message, __construct, arginfo_phalcon_validation_message___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Validation_Message, setType, arginfo_phalcon_validation_message_settype, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Validation_Message, getType, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Validation_Message, setCode, arginfo_phalcon_validation_message_setcode, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Validation_Message, getCode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Validation_Message, setMessage, arginfo_phalcon_validation_message_setmessage, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Validation_Message, getMessage, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Validation_Message, setField, arginfo_phalcon_validation_message_setfield, ZEND_ACC_PUBLIC)
Expand Down
5 changes: 5 additions & 0 deletions unit-tests/FormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,25 @@ public function testFormValidator()
'_type' => 'PresenceOf',
'_message' => 'The telephone is required',
'_field' => 'telephone',
'_code' => 0,
)),
1 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'TooShort',
'_message' => 'Value of field \'telephone\' is less than the minimum 5 characters',
'_field' => 'telephone',
'_code' => 0,
)),
2 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'Regex',
'_message' => 'The telephone has an invalid format',
'_field' => 'telephone',
'_code' => 0,
)),
3 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'PresenceOf',
'_message' => 'The address is required',
'_field' => 'address',
'_code' => 0,
)),
),
)
Expand All @@ -294,6 +298,7 @@ public function testFormValidator()
'_type' => 'Regex',
'_message' => 'The telephone has an invalid format',
'_field' => 'telephone',
'_code' => 0,
)),
),
));
Expand Down
28 changes: 28 additions & 0 deletions unit-tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ public function testValidationPresenceOf()
'_type' => 'PresenceOf',
'_message' => 'name is required',
'_field' => 'name',
'_code' => '0',
)),
1 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'PresenceOf',
'_message' => 'last_name is required',
'_field' => 'last_name',
'_code' => '0',
)),
)
));
Expand All @@ -101,6 +103,7 @@ public function testValidationPresenceOf()
'_type' => 'PresenceOf',
'_message' => 'name is required',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -130,11 +133,13 @@ public function testValidationPresenceOfCustomMessage()
'_type' => 'PresenceOf',
'_message' => 'The name is required',
'_field' => 'name',
'_code' => '0',
)),
1 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'PresenceOf',
'_message' => 'The last name is required',
'_field' => 'last_name',
'_code' => '0',
)),
)
));
Expand All @@ -151,6 +156,7 @@ public function testValidationPresenceOfCustomMessage()
'_type' => 'PresenceOf',
'_message' => 'The name is required',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand All @@ -176,6 +182,7 @@ public function testValidationIdentical()
'_type' => 'Identical',
'_message' => 'name does not have the expected value',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -208,6 +215,7 @@ public function testValidationIdenticalCustomMessage()
'_type' => 'Identical',
'_message' => 'The name must be peter',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -239,6 +247,7 @@ public function testValidationRegex()
'_type' => 'Regex',
'_message' => 'Value of field \'car_plate\' doesn\'t match regular expression',
'_field' => 'car_plate',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -271,6 +280,7 @@ public function testValidationRegexCustomMessage()
'_type' => 'Regex',
'_message' => 'The car plate is not valid',
'_field' => 'car_plate',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -300,6 +310,7 @@ public function testValidationEmail()
'_type' => 'Email',
'_message' => 'Value of field \'email\' must have a valid e-mail format',
'_field' => 'email',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -337,6 +348,7 @@ public function testValidationEmailCustomMessage()
'_type' => 'Email',
'_message' => 'The email is not valid',
'_field' => 'email',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -374,6 +386,7 @@ public function testValidationInclusionIn()
'_type' => 'InclusionIn',
'_message' => 'Value of field \'status\' must be part of list: A, I',
'_field' => 'status',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -412,6 +425,7 @@ public function testValidationInclusionInCustomMessage()
'_type' => 'InclusionIn',
'_message' => 'The status must be A=Active or I=Inactive',
'_field' => 'status',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -449,6 +463,7 @@ public function testValidationExclusionIn()
'_type' => 'ExclusionIn',
'_message' => 'Value of field \'status\' must not be part of list: A, I',
'_field' => 'status',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -487,6 +502,7 @@ public function testValidationExclusionInCustomMessage()
'_type' => 'ExclusionIn',
'_message' => 'The status must not be A=Active or I=Inactive',
'_field' => 'status',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -524,6 +540,7 @@ public function testValidationStringLengthMinimum()
'_type' => 'TooShort',
'_message' => 'Value of field \'name\' is less than the minimum 3 characters',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -562,6 +579,7 @@ public function testValidationStringLengthMinimumCustomMessage()
'_type' => 'TooShort',
'_message' => 'The message is too short',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -599,6 +617,7 @@ public function testValidationStringLengthMaximum()
'_type' => 'TooLong',
'_message' => 'Value of field \'name\' exceeds the maximum 4 characters',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -631,6 +650,7 @@ public function testValidationStringLengthMaximumCustomMessage()
'_type' => 'TooLong',
'_message' => 'The message is too long',
'_field' => 'name',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -663,6 +683,7 @@ public function testValidationBetween()
'_type' => 'Between',
'_message' => 'price is not between a valid range',
'_field' => 'price',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -702,6 +723,7 @@ public function testValidationBetweenCustomMessage()
'_type' => 'Between',
'_message' => 'The price must be between 1 and 3',
'_field' => 'price',
'_code' => '0',
))
)
));
Expand Down Expand Up @@ -745,16 +767,19 @@ public function testValidationMixed()
'_type' => 'PresenceOf',
'_message' => 'The name is required',
'_field' => 'name',
'_code' => '0',
)),
1 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'PresenceOf',
'_message' => 'The email is required',
'_field' => 'email',
'_code' => '0',
)),
2 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'PresenceOf',
'_message' => 'The login is required',
'_field' => 'login',
'_code' => '0',
)),
),
));
Expand Down Expand Up @@ -787,11 +812,13 @@ public function testValidationCancelOnFail()
'_type' => 'PresenceOf',
'_message' => 'The name is required',
'_field' => 'name',
'_code' => '0',
)),
1 => Phalcon\Validation\Message::__set_state(array(
'_type' => 'PresenceOf',
'_message' => 'The email is required',
'_field' => 'email',
'_code' => '0',
))
),
));
Expand Down Expand Up @@ -830,6 +857,7 @@ public function testValidationFiltering()
'_type' => 'PresenceOf',
'_message' => 'The email is required',
'_field' => 'email',
'_code' => '0',
))
);

Expand Down