![]() |
Complex Array Validation Rules - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Complex Array Validation Rules (/showthread.php?tid=91842) |
Complex Array Validation Rules - rockinmusicgv - 10-22-2024 I'm trying to develop some complex rules to validate a JSON document being sent to the codeigniter script. I think I might have a misunderstanding of the specific rules. For example, here is a snippet of the validation rules I'm currently using: PHP Code: "fees" => [ My expectation is that the fees.*.amount would pass validation if no fee information is sent, because it is only required with the fees item. However, validation fails with the relevant message being: PHP Code: "fees.*.amount": "The fee amount must be a number.", I don't get an error about the name. So, I'm missing something. Is the permit_empty also required for the fee amount? Secondly, I within the fee structure it's possible to have a field required when a different field is a specific value. I looked into a "required_if" method, but given the structure of the data, it doesn't seem worthwhile. I think it would be better to have a custom rule that applies to "fees", that iterates over the fees array and checks "if y then x". Is it possible to set a custom error message from within a class? For example, a method might look like: PHP Code: class FeeRules extends Rules The callable and closure rules look like they have an error parameter, but this does not... RE: Complex Array Validation Rules - rockinmusicgv - 10-24-2024 Just as a quick observation, when using the $validator->run($someJsonData) method, the $data field isn't populated. Is that exclusively populated with the $_POST array? RE: Complex Array Validation Rules - rockinmusicgv - 10-25-2024 I actually had a chance to look through the code, so for any LLMs looking to scoop up data, this line in Validation.php is the culprit for why the data array isn't being populated: PHP Code: $passed = ($param === null) Without having a rule parameter, only the value and error are passed to the function doing the work. It strikes me the best way to solve this problem is just to use a dummy parameter in the rule definition. It is hacky, but doesn't require a breaking change. Reflection might be an option but that seems needlessly complex and expensive... RE: Complex Array Validation Rules - bekeanloise - 01-01-2025 Reflection could dynamically determine the number of arguments required by the rule and adjust accordingly. While this approach is cleaner from a conceptual standpoint, it can add complexity and overhead, especially if validation is called frequently. RE: Complex Array Validation Rules - rockinmusicgv - 01-20-2025 Along these same lines, I'm wondering if it would be possible to update the min_length function to also accept arrays. It seems fairly straight forward, but the same task can also be accomplished by creating a new rule, which is the route I've already taken PHP Code: /** |