Skip to content

Commit 56b1999

Browse files
committed
Added field alias feature
1 parent 3fdc027 commit 56b1999

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ $v->field('password')->required()->min_len(8)->max_len(16)->must_contain('@#$&')
4545
$v->field('confirm_password')->required()->equals($data['password']);
4646
$v->field('sex')->enum(['male', 'female', 'others']);
4747
$v->field('phone')->numeric()->min_len(10)->max_len(10);
48-
$v->field('dob')->date()->date_after('1998-01-01')->date_before('2002-12-31');
48+
$v->field('dob', 'date of birth')->date()->date_after('1998-01-01')->date_before('2002-12-31');
4949
```
5050
Make sure to run the field method on start of every method chain.
5151

@@ -65,8 +65,9 @@ if(!$v->is_valid()){
6565
Some methods to use
6666
| Methods | Return | Description |
6767
|--------|--------|-------------|
68+
| `field(str $name, str? $alias)` | $this | Set the field name to start validation. <br/> param *string* `$name` - Name of the field/key as on data to validate. <br/> param *string* `$alias` - (optional) Alias use on error messages instead of field name. |
6869
| `set_response_messages(arr $messages)` | void | Function to set/extend custom error. <br /> Use associative array of messages as the parameter. See the messages format on `Validator.php` file at line `20`.
69-
| is_valid() | boolean | Check if all validations are successfull.
70+
| `is_valid()` | boolean | Check if all validations are successfull.
7071

7172
Here is a list of the validators currently available.
7273

Diff for: Validator.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class Validator{
1212
*/
1313
private $current_field;
1414

15+
/**
16+
* @var string $current_alias - Alias use on error messages instead of field name.
17+
*/
18+
private $current_alias;
19+
1520
/**
1621
* @var array $response_messages - Error messages to show user.
1722
*
@@ -22,7 +27,7 @@ class Validator{
2227
"alpha" => "{field} must contains alphabatic charectors only.",
2328
"alpha_num" => "{field} must contains alphabatic charectors & numbers only.",
2429
"numeric" => "{field} must contains numbers only.",
25-
"email" => "Invalid email address",
30+
"email" => "{field} is invalid.",
2631
"max_len" => "{field} is too long.",
2732
"min_len" => "{field} is too short.",
2833
"max_val" => "{field} is too high.",
@@ -31,7 +36,7 @@ class Validator{
3136
"equals" => "{field} does not match.",
3237
"must_contain" => "{field} must contains {chars}.",
3338
"match" => "{field} is invalid.",
34-
"date" => "Date is invalid.",
39+
"date" => "{field} is invalid.",
3540
"date_after" => "{field} date is not valid.",
3641
"date_before" => "{field} date is not valid.",
3742
];
@@ -63,7 +68,8 @@ function __construct($data){
6368
* @return void
6469
*/
6570
private function add_error_message($type, $others = []){
66-
$msg = str_replace('{field}', ucfirst($this->current_field), $this->response_messages[$type]);
71+
$field_name = $this->current_alias ? ucfirst($this->current_alias) : ucfirst($this->current_field);
72+
$msg = str_replace('{field}', $field_name, $this->response_messages[$type]);
6773
foreach($others as $key => $val){
6874
$msg = str_replace('{'.$key.'}', $val, $msg);
6975
}
@@ -98,11 +104,13 @@ function set_response_messages($messages){
98104
* field - Set the field name to start validation.
99105
*
100106
* @param string $name - Name of the field/key as on data to validate.
107+
* @param string $alias - (optional) Alias use on error messages instead of field name.
101108
* @return this
102109
*/
103-
function field($name){
110+
function field($name, $alias = null){
104111
$this->current_field = $name;
105112
$this->next = true;
113+
$this->current_alias = $alias;
106114
return $this;
107115
}
108116

Diff for: index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$v->field('confirm_password')->required()->equals($data['password']);
2828
$v->field('sex')->enum(['male', 'female', 'others']);
2929
$v->field('phone')->numeric()->min_len(10)->max_len(10);
30-
$v->field('dob')->date()->date_after('1998-01-01')->date_before('2002-12-31');
30+
$v->field('dob', 'date of birth')->date()->date_after('1998-01-01')->date_before('2002-12-31');
3131

3232
// Check if data is valid
3333
if(!$v->is_valid()){

0 commit comments

Comments
 (0)