Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array validation misapplied #333

Closed
bytestream opened this issue Jun 26, 2018 · 4 comments · Fixed by #334
Closed

Array validation misapplied #333

bytestream opened this issue Jun 26, 2018 · 4 comments · Fixed by #334

Comments

@bytestream
Copy link
Collaborator

bytestream commented Jun 26, 2018

Laravel: 5.5.40
jsvalidation: latest

Support to validate array fields using non array rules was added in 4bf612a under #206

I have inputs such as:
<input name="emails[0][id]" type="hidden" value="">
<input size="30" name="emails[0][address]" type="email" value="" id="emails[0][address]">

Which is validated with:
'emails' => 'required_if:parent,0|nullable|array'

$emailRules = [];
foreach ($this->request->get('emails', []) as $k => $v) {
    $emailRules["emails.$k.address"] = 'required_if:parent,0|nullable|email';

The emails rule is getting applied to both elements above, because of

return new RegExp('^'+regexpParts.join('.*')+'$');
which produces: ^emails\[.*\]$ and matches both of the above elements.

I don't expect the emails rule to be applied to all children. I suspect, it's a special case as it was introduced to cater for: #180. With this in mind, the regex should be

return new RegExp('^'+regexpParts.join('[^\\]]*')+'$');

so it only matches single dimensional arrays. At the moment the greedy quantifier means it's matching [0][id]

bytestream pushed a commit to bytestream/laravel-jsvalidation that referenced this issue Jun 26, 2018
bytestream pushed a commit to bytestream/laravel-jsvalidation that referenced this issue Jun 26, 2018
bytestream added a commit to bytestream/laravel-jsvalidation that referenced this issue Jun 26, 2018
* Fixed proengsoft#333

* Updated jsvalidation.min.js
@maganius
Copy link

I have the same issue the form request array does not appear on javascript code it's empty and the fixes don't work for me. im using laravel 5.5.4

@bytestream
Copy link
Collaborator Author

@maganius please provide a test case, the fix works fine for the case I wrote above

@maganius
Copy link

I have selects such as:
<select class="form-control" id="service_type" name="services[0][service_type]">

In the FormRequest validator

public function rules() { return [ 'port' => 'required', 'services.*.service_type' => 'required|array', 'services.*.subservice_type' => 'required|array', ]; }

I tried change the RegExp and
the JsValidatorFactory.php where this replace * for # <- with this in the js validator appear the rule but it no validate the selects. #298

The first select is static but I have a button to add more select dynamically

@bytestream
Copy link
Collaborator Author

bytestream commented Jul 21, 2018

Yeah your issue is #297

This (#333) is a different issue

@bytestream bytestream linked a pull request Mar 13, 2020 that will close this issue
bytestream pushed a commit to bytestream/laravel-jsvalidation that referenced this issue Mar 15, 2020
bytestream added a commit that referenced this issue Mar 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants