I have 2 field say var1
and var2
. both are text fields what I want if var2 contains a word __tocken_
then field var1
is required.
I tried this but it is not working,
$validator = Validator::make(
['var1' => 'required_if:var2,regex:/__tocken_/']
//['var2' => ['required', 'regex:/__tocken_/']] //<--Regex is working fine here
);
So what I'm doing right now I'm setting a 3rd variable var3
and on form submit I'm doing a JS validation if var2
contains a word with pattern __tocken_
then I'm setting var3
as 1
otherwise 0
. And the validation rule is
$validator = Validator::make(
['var1' => 'required_if:var3,1'] //working fine
);
So my question is it possible to have regex NOT the exact value in required_if
validation rule?
Sample possible value for var2
- Hi
__FIRST_NAME__
, lorem ispam__tocken_Ur1vG6xK__
. - lorem ispam
__tocken_456vG6xK__
lorem__tocken_T57kq6xK__
- lorem ispam.. so on
var2
as such:['var2' => 'sometimes|regex:/__tocken_/|required_with:var1']
– revovar2
is fully optional, only if it has a above mention pattern thenvar1
is needed. So I was adding validations tovar1
. Moreover I cannot able to make your suggestion to working. – Raunak Guptasometimes
rule. If it has a value then it should contain__tocken_
to makevar1
a required field as well. – revo