function __construct()
 {
     Validator::extend('multiple_value_in', function ($attribute, $value, $parameters, $validator) {
         $inputs = explode(';', $value);
         foreach ($inputs as $input) {
             if (!in_array($input, $parameters)) {
                 return false;
             }
         }
         return true;
     });
     Validator::extendImplicit('required_any', function ($attribute, $value, $parameters, $validator) {
         for ($i = 1; $i < count($parameters); $i = $i + 2) {
             $values = $parameters[$i];
             if (!empty($values)) {
                 return true;
             }
         }
         return false;
     });
     Validator::extendImplicit('required_only_one', function ($attribute, $value, $parameters, $validator) {
         $counter = 0;
         foreach ($parameters as $parameterIndex => $parameter) {
             if ($parameterIndex % 2 != 0 && !empty($parameter)) {
                 $counter++;
             }
         }
         if ($counter == self::REQUIRED_NONEMPTY_FIELD) {
             return true;
         }
         return false;
     });
     Validator::extendImplicit('unique_validation', function ($attribute, $value, $parameters, $validator) {
         $csvDatas = Excel::load($parameters[2])->get()->toArray();
         $counter = 0;
         $csvFiled = $parameters[3];
         foreach ($csvDatas as $csvDataIndex => $csvData) {
             if ($csvData[$csvFiled] == $parameters[1]) {
                 $counter++;
             }
         }
         if ($counter == self::IDENTICAL_INTERNAL_REFERENCE) {
             return true;
         }
         return false;
     });
 }
 function __construct()
 {
     Validator::extendImplicit('unique_lang', function ($attribute, $value, $parameters, $validator) {
         $languages = [];
         foreach ($value as $narrative) {
             $language = $narrative['language'];
             if (in_array($language, $languages)) {
                 return false;
             }
             $languages[] = $language;
         }
         return true;
     });
     Validator::extendImplicit('required_with_language', function ($attribute, $value, $parameters, $validator) {
         $language = preg_replace('/([^~]+).narrative/', '$1.language', $attribute);
         return !(Input::get($language) && !Input::get($attribute));
     });
 }
 public function __construct()
 {
     Validator::extendImplicit('required_any', function ($attribute, $value, $parameters, $validator) {
         for ($i = 1; $i < count($parameters); $i = $i + 2) {
             $value = $parameters[$i];
             if (!empty($value)) {
                 return true;
             }
         }
         return false;
     });
     Validator::extend('code_list', function ($attribute, $value, $parameters, $validator) {
         $listName = $parameters[1];
         $listType = $parameters[0];
         $codeList = $this->getCodes($listName, $listType);
         return !array_diff(explode(';', $value), $codeList);
     });
     Validator::extend('unique_activity_identifier', function ($attribute, $value, $parameters, $validator) {
         $table = 'activity_data';
         $column = 'identifier';
         $jsonPath = '{activity_identifier}';
         $orgId = session('org_id');
         $builder = DB::table($table)->whereRaw(sprintf("%s #>> '{%s}' = ?", $column, str_replace('.', ',', $jsonPath)), [$value])->where('organization_id', $orgId);
         $count = $builder->count();
         $validator->addReplacer('unique_activity_identifier', function ($message, $attribute, $rule, $parameters) use($validator, $builder) {
             $activity = $builder->first();
             if ($activity) {
                 $activityId = $activity->id;
                 $identifier = json_decode($activity->identifier, true);
                 $activityTitle = $activity->title ? json_decode($activity->title, true)[0]['narrative'] : $identifier['iati_identifier_text'];
                 $message = str_replace(':activity_identifier', $identifier['activity_identifier'], $message);
                 $message = str_replace(':activity_id', $activityId, $message);
                 $message = str_replace(':activity_title', $activityTitle, $message);
             }
             return $message;
         });
         return $count === 0;
     });
     Validator::extend('is_single_value', function ($attribute, $value, $parameters, $validator) {
         return count(explode(';', $value)) == 1;
     });
 }
 /**
  * Get the validation rules that apply to the request.
  *
  * @return array
  */
 public function rules()
 {
     Validator::extendImplicit('diff', function ($attribute, $value, $parameters, $validator) {
         return false;
     });
     $req = $this;
     $this->reporting_organization_info = $req->get('reporting_organization_info')[0];
     $rules = [];
     foreach ($this->reporting_organization_info as $key => $val) {
         $rules["reporting_organization_info.0.{$key}"] = 'required';
         $rules["reporting_organization_info.0.narrative"] = "unique_lang|unique_default_lang";
     }
     $rules["reporting_organization_info.0.narrative.0.narrative"] = 'required';
     $rules["default_field_values.0.linked_data_uri"] = 'url';
     $rules["default_field_values.0.default_currency"] = 'required';
     $rules["default_field_values.0.default_language"] = 'required';
     $rules["default_field_values.0.default_hierarchy"] = 'numeric';
     $rules = array_merge($rules, $this->getRulesForReportingOrganization($this->reporting_organization_info));
     return $rules;
 }
Example #5
0
 function __construct()
 {
     parent::__construct();
     Validator::extendImplicit('year_value_narrative_validation', function ($attribute, $value, $parameters, $validator) {
         $narratives = $value['comment'][0]['narrative'];
         $hasNarrative = false;
         foreach ($narratives as $narrative) {
             if ($narrative['narrative']) {
                 $hasNarrative = true;
                 break;
             }
         }
         if (!$hasNarrative) {
             return true;
         }
         isset($value['year']) ?: ($value['year'] = null);
         isset($value['value']) ?: ($value['value'] = null);
         return $hasNarrative && ($value['year'] || $value['value']);
     });
 }
 function __construct()
 {
     Validator::extendImplicit('unique_lang', function ($attribute, $value, $parameters, $validator) {
         $languages = [];
         foreach ($value as $narrative) {
             $language = $narrative['language'];
             if (in_array($language, $languages)) {
                 return false;
             }
             $languages[] = $language;
         }
         return true;
     });
     Validator::extendImplicit('unique_default_lang', function ($attribute, $value, $parameters, $validator) {
         $languages = [];
         $defaultLanguage = getDefaultLanguage();
         $validator->addReplacer('unique_default_lang', function ($message, $attribute, $rule, $parameters) use($validator, $defaultLanguage) {
             return str_replace(':language', app(GetCodeName::class)->getActivityCodeName('Language', $defaultLanguage), $message);
         });
         $check = true;
         foreach ($value as $narrative) {
             $languages[] = $narrative['language'];
         }
         if (count($languages) === count(array_unique($languages))) {
             if (in_array("", $languages) && in_array($defaultLanguage, $languages)) {
                 $check = false;
             }
         }
         return $check;
     });
     Validator::extendImplicit('sum', function ($attribute, $value, $parameters, $validator) {
         return false;
     });
     Validator::extendImplicit('required_with_language', function ($attribute, $value, $parameters, $validator) {
         $language = preg_replace('/([^~]+).narrative/', '$1.language', $attribute);
         return !(Input::get($language) && !Input::get($attribute));
     });
     Validator::extend('exclude_operators', function ($attribute, $value, $parameters, $validator) {
         return !preg_match('/[\\/\\&\\|\\?|]+/', $value);
     });
 }