Handle regex format when assigned from API (#9443)

This commit is contained in:
Ivan Nieto Vivanco 2021-04-20 16:44:47 -05:00 committed by GitHub
parent e4acf8d795
commit 36bc47c61c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View file

@ -95,7 +95,14 @@ class CustomFieldsController extends Controller
$field = new CustomField; $field = new CustomField;
$data = $request->all(); $data = $request->all();
$validator = Validator::make($data, $field->validationRules()); $regex_format = null;
if (str_contains($data["format"], "regex:")){
$regex_format = $data["format"];
}
$validator = Validator::make($data, $field->validationRules($regex_format));
if ($validator->fails()) { if ($validator->fails()) {
return response()->json(Helper::formatStandardApiResponse('error', null, $validator->errors())); return response()->json(Helper::formatStandardApiResponse('error', null, $validator->errors()));
} }

View file

@ -353,7 +353,7 @@ class CustomField extends Model
* @since [v4.1.10] * @since [v4.1.10]
* @return array * @return array
*/ */
public function validationRules() public function validationRules($regex_format = null)
{ {
return [ return [
"name" => "required|unique:custom_fields", "name" => "required|unique:custom_fields",
@ -362,7 +362,7 @@ class CustomField extends Model
Rule::in(['text', 'listbox', 'textarea', 'checkbox', 'radio']) Rule::in(['text', 'listbox', 'textarea', 'checkbox', 'radio'])
], ],
'format' => [ 'format' => [
Rule::in(array_merge(array_keys(CustomField::PREDEFINED_FORMATS), CustomField::PREDEFINED_FORMATS)) Rule::in(array_merge(array_keys(CustomField::PREDEFINED_FORMATS), CustomField::PREDEFINED_FORMATS, [$regex_format]))
], ],
'field_encrypted' => "nullable|boolean" 'field_encrypted' => "nullable|boolean"
]; ];