Skip to content

Commit

Permalink
feat: allow greater or equal validation (#279)
Browse files Browse the repository at this point in the history
Signed-off-by: romanetar <[email protected]>
  • Loading branch information
romanetar authored Aug 27, 2024
1 parent f07e763 commit dde1c9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public static function build(array $data, $update = false){
'use_speakers' => 'sometimes|boolean',
'are_speakers_mandatory' => 'sometimes|boolean|required_with:use_speakers',
'min_speakers' => 'sometimes|integer|required_with:use_speakers',
'max_speakers' => 'sometimes|integer|required_with:use_speakers|greater_than_field:min_speakers',
'max_speakers' => 'sometimes|integer|required_with:use_speakers|greater_or_equal_than_field:min_speakers',
'use_moderator' => 'sometimes|boolean',
'is_moderator_mandatory' => 'sometimes|boolean|required_with:use_moderator',
'min_moderators' => 'sometimes|integer|required_with:use_moderator',
'max_moderators' => 'sometimes|integer|required_with:use_moderator|greater_than_field:min_moderators',
'max_moderators' => 'sometimes|integer|required_with:use_moderator|greater_or_equal_than_field:min_moderators',
'should_be_available_on_cfp' => 'sometimes|boolean',
'moderator_label' => 'sometimes|string',
'allow_custom_ordering' => 'sometimes|boolean',
Expand Down
15 changes: 14 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public function boot()

Validator::extend('greater_than_field', function ($attribute, $value, $parameters, $validator) {
$validator->addReplacer('greater_than_field', function ($message, $attribute, $rule, $parameters) use ($validator) {
return sprintf("%s should be greather than %s", $attribute, $parameters[0]);
return sprintf("%s should be greater than %s", $attribute, $parameters[0]);
});
$data = $validator->getData();
if (is_null($value) || intval($value) == 0) return true;
Expand All @@ -534,6 +534,19 @@ public function boot()
return true;
});

Validator::extend('greater_or_equal_than_field', function ($attribute, $value, $parameters, $validator) {
$validator->addReplacer('greater_or_equal_than_field', function ($message, $attribute, $rule, $parameters) use ($validator) {
return sprintf("%s should be greater or equal than %s", $attribute, $parameters[0]);
});
$data = $validator->getData();
if (is_null($value) || intval($value) == 0) return true;
if (isset($data[$parameters[0]])) {
$compare_to = $data[$parameters[0]];
return intval($compare_to) <= intval($value);
}
return true;
});

Validator::extend('valid_epoch', function ($attribute, $value, $parameters, $validator) {
$validator->addReplacer('valid_epoch', function ($message, $attribute, $rule, $parameters) use ($validator) {
return sprintf("%s should be a valid epoch value", $attribute);
Expand Down

0 comments on commit dde1c9b

Please sign in to comment.