Skip to content

Commit

Permalink
Merge pull request phalcon-orphanage#948 from Jurigag/callback-validator
Browse files Browse the repository at this point in the history
Callback validator 3.1.x docs
  • Loading branch information
niden authored Mar 17, 2017
2 parents 2db6b7f + cc6b388 commit af408f1
Show file tree
Hide file tree
Showing 11 changed files with 672 additions and 0 deletions.
67 changes: 67 additions & 0 deletions en/reference/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Phalcon exposes a set of built-in validators for this component:
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\CreditCard <../api/Phalcon_Validation_Validator_CreditCard>` | Validates a credit card number |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\Callback <../api/Phalcon_Validation_Validator_Callback>` | Validates using callback function |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+

The following example explains how to create additional validators for this component:

Expand Down Expand Up @@ -202,6 +204,71 @@ The following example explains how to create additional validators for this comp
It is important that validators return a valid boolean value indicating if the validation was successful or not.

Callback Validator
------------------
By using :doc:`Phalcon\\Validation\Validator\Callback <../api/Phalcon_Validation_Validator_Callback>` you can execute custom
function which must return boolean or new validator class which will be used to validate the same field. By returning :code:`true`
validation will be successful, returning :code:`false` will mean validation failed. When executing this validator Phalcon will pass
data depending what it is - if it's an entity then entity will be passed, otherwise data. There is example:

.. code-block:: php
<?php
use \Phalcon\Validation;
use \Phalcon\Validation\Validator\Callback;
use \Phalcon\Validation\Validator\PresenceOf;
$validation = new Validation();
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
return $data["amount"] % 2 == 0;
},
"message" => "Only even number of products are accepted"
]
)
);
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
if($data["amount"] % 2 == 0) {
return $data["amount"] != 2;
}
return true;
},
"message" => "You can't buy 2 products"
]
)
);
$validation->add(
"description",
new Callback(
[
"callback" => function($data) {
if($data["amount"] >= 10) {
return new PresenceOf(
[
"message" => "You must write why you need so big amount."
]
);
}
return true;
}
]
)
);
$messages = $validation->validate(["amount" => 1]); // will return message from first validator
$messages = $validation->validate(["amount" => 2]); // will return message from second validator
$messages = $validation->validate(["amount" => 10]); // will return message from validator returned by third validator
Validation Messages
-------------------
:doc:`Phalcon\\Validation <../api/Phalcon_Validation>` has a messaging subsystem that provides a flexible way to output or store the
Expand Down
67 changes: 67 additions & 0 deletions es/reference/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Phalcon da un conjunto de validators ya creados:
+--------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\CreditCard <../api/Phalcon_Validation_Validator_CreditCard>` | Valida el número de tarjeta de crédito/débito |
+--------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\Callback <../api/Phalcon_Validation_Validator_Callback>` | Validates using callback function |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+

El siguiente ejemplo explica como crear un nuevo validator para este componente:

Expand Down Expand Up @@ -202,6 +204,71 @@ El siguiente ejemplo explica como crear un nuevo validator para este componente:
Es importante que los validators devuelvan un valor booleano correcto indicando si la validación fue pasada con éxito o no.

Callback Validator
------------------
By using :doc:`Phalcon\\Validation\Validator\Callback <../api/Phalcon_Validation_Validator_Callback>` you can execute custom
function which must return boolean or new validator class which will be used to validate the same field. By returning :code:`true`
validation will be successful, returning :code:`false` will mean validation failed. When executing this validator Phalcon will pass
data depending what it is - if it's an entity then entity will be passed, otherwise data. There is example:

.. code-block:: php
<?php
use \Phalcon\Validation;
use \Phalcon\Validation\Validator\Callback;
use \Phalcon\Validation\Validator\PresenceOf;
$validation = new Validation();
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
return $data["amount"] % 2 == 0;
},
"message" => "Only even number of products are accepted"
]
)
);
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
if($data["amount"] % 2 == 0) {
return $data["amount"] != 2;
}
return true;
},
"message" => "You can't buy 2 products"
]
)
);
$validation->add(
"description",
new Callback(
[
"callback" => function($data) {
if($data["amount"] >= 10) {
return new PresenceOf(
[
"message" => "You must write why you need so big amount."
]
);
}
return true;
}
]
)
);
$messages = $validation->validate(["amount" => 1]); // will return message from first validator
$messages = $validation->validate(["amount" => 2]); // will return message from second validator
$messages = $validation->validate(["amount" => 10]); // will return message from validator returned by third validator
Mensajes de validación
----------------------
:doc:`Phalcon\\Validation <../api/Phalcon_Validation>` tiene un subsistema que provee una forma flexible de salida o de almacenar los mensajes de validación generados durante el proceso.
Expand Down
67 changes: 67 additions & 0 deletions fa/reference/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Phalcon exposes a set of built-in validators for this component:
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\CreditCard <../api/Phalcon_Validation_Validator_CreditCard>` | Validates a credit card number |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\Callback <../api/Phalcon_Validation_Validator_Callback>` | Validates using callback function |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+

The following example explains how to create additional validators for this component:

Expand Down Expand Up @@ -202,6 +204,71 @@ The following example explains how to create additional validators for this comp
It is important that validators return a valid boolean value indicating if the validation was successful or not.

Callback Validator
------------------
By using :doc:`Phalcon\\Validation\Validator\Callback <../api/Phalcon_Validation_Validator_Callback>` you can execute custom
function which must return boolean or new validator class which will be used to validate the same field. By returning :code:`true`
validation will be successful, returning :code:`false` will mean validation failed. When executing this validator Phalcon will pass
data depending what it is - if it's an entity then entity will be passed, otherwise data. There is example:

.. code-block:: php
<?php
use \Phalcon\Validation;
use \Phalcon\Validation\Validator\Callback;
use \Phalcon\Validation\Validator\PresenceOf;
$validation = new Validation();
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
return $data["amount"] % 2 == 0;
},
"message" => "Only even number of products are accepted"
]
)
);
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
if($data["amount"] % 2 == 0) {
return $data["amount"] != 2;
}
return true;
},
"message" => "You can't buy 2 products"
]
)
);
$validation->add(
"description",
new Callback(
[
"callback" => function($data) {
if($data["amount"] >= 10) {
return new PresenceOf(
[
"message" => "You must write why you need so big amount."
]
);
}
return true;
}
]
)
);
$messages = $validation->validate(["amount" => 1]); // will return message from first validator
$messages = $validation->validate(["amount" => 2]); // will return message from second validator
$messages = $validation->validate(["amount" => 10]); // will return message from validator returned by third validator
Validation Messages
-------------------
:doc:`Phalcon\\Validation <../api/Phalcon_Validation>` has a messaging subsystem that provides a flexible way to output or store the
Expand Down
67 changes: 67 additions & 0 deletions fr/reference/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Phalcon exposes a set of built-in validators for this component:
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\CreditCard <../api/Phalcon_Validation_Validator_CreditCard>` | Validates a credit card number |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\Callback <../api/Phalcon_Validation_Validator_Callback>` | Validates using callback function |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+

The following example explains how to create additional validators for this component:

Expand Down Expand Up @@ -202,6 +204,71 @@ The following example explains how to create additional validators for this comp
It is important that validators return a valid boolean value indicating if the validation was successful or not.

Callback Validator
------------------
By using :doc:`Phalcon\\Validation\Validator\Callback <../api/Phalcon_Validation_Validator_Callback>` you can execute custom
function which must return boolean or new validator class which will be used to validate the same field. By returning :code:`true`
validation will be successful, returning :code:`false` will mean validation failed. When executing this validator Phalcon will pass
data depending what it is - if it's an entity then entity will be passed, otherwise data. There is example:

.. code-block:: php
<?php
use \Phalcon\Validation;
use \Phalcon\Validation\Validator\Callback;
use \Phalcon\Validation\Validator\PresenceOf;
$validation = new Validation();
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
return $data["amount"] % 2 == 0;
},
"message" => "Only even number of products are accepted"
]
)
);
$validation->add(
"amount",
new Callback(
[
"callback" => function($data) {
if($data["amount"] % 2 == 0) {
return $data["amount"] != 2;
}
return true;
},
"message" => "You can't buy 2 products"
]
)
);
$validation->add(
"description",
new Callback(
[
"callback" => function($data) {
if($data["amount"] >= 10) {
return new PresenceOf(
[
"message" => "You must write why you need so big amount."
]
);
}
return true;
}
]
)
);
$messages = $validation->validate(["amount" => 1]); // will return message from first validator
$messages = $validation->validate(["amount" => 2]); // will return message from second validator
$messages = $validation->validate(["amount" => 10]); // will return message from validator returned by third validator
Validation Messages
-------------------
:doc:`Phalcon\\Validation <../api/Phalcon_Validation>` has a messaging subsystem that provides a flexible way to output or store the
Expand Down
2 changes: 2 additions & 0 deletions id/reference/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Phalcon exposes a set of built-in validators for this component:
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\CreditCard <../api/Phalcon_Validation_Validator_CreditCard>` | Validates a credit card number |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
| :doc:`Phalcon\\Validation\\Validator\\Callback <../api/Phalcon_Validation_Validator_Callback>` | Validates using callback function |
+--------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+

The following example explains how to create additional validators for this component:

Expand Down
Loading

0 comments on commit af408f1

Please sign in to comment.