Here's what I came up with:
class RegisterController extends Controller
{
protected function validator(array $data)
{
$rules = merge_validation_rules((new User)->getRules(), [
'email' => 'required',
'password' => 'min:6',
]);
return Validator::make($data, $rules);
}
...
merge_validation_rules
helper:
function merge_validation_rules($a, $b)
{
$r = $a;
foreach ($b as $k => $v)
if (isset($a[$k])) {
$r[$k] = array_merge(explode('|', $a[$k]), explode('|', $b[$k]));
if (in_array('sometimes', $r[$k])
&& in_array('required', $r[$k]))
$r[$k] = array_diff($r[$k], ['sometimes']);
$r[$k] = implode('|', $r[$k]);
} else
$r[$k] = $b[$k];
return $r;
}
merge_validation_rules
test:
<?php
namespace Tests;
use Tests\TestCase;
class HelpersTest extends TestCase
{
/**
* @dataProvider mergeValidationRulesProvider
*/
function testMergeValidationRules($input, $output)
{
$r = call_user_func_array('merge_validation_rules', $input);
$this->assertEquals($output, $r);
}
function mergeValidationRulesProvider() { return [
[[['a' => 'ra1', 'b' => 'rb1'], ['a' => 'ra2', 'c' => 'rc1']],
['a' => 'ra1|ra2', 'b' => 'rb1', 'c' => 'rc1']],
[[['a' => 'sometimes'], ['a' => 'required']],
['a' => 'required']],
]; }
}
Here you can see one more issue. I'm using both user/password and social logins. So email is not always present (not every social networks returns it). So in the model I have 'email' => 'sometimes|email'
, but in registration controller I want it to be required
. For now I remove sometimes
if required
is present. We'll see if it'll work out.