Home > database >  How to set a dynamic length in laravel-nova?
How to set a dynamic length in laravel-nova?

Time:01-13

I am using Nova in laravel for designing the page,i am writing some rules function which will allows digits:20 .

 Text::make('TrackID','id')->rules('nullable','digits:20' )->sortable()->nullable()->hideFromIndex(),

while updating the data if the numbers are more that it should throw an error like Max 20 digits allowed, now i want to give the length dynamically ,How to do that one

How i am trying

$length=25;

 Text::make('TrackID','id')->rules('nullable',digits:$this->length )->sortable()->nullable()->hideFromIndex(),

Now the error is Max $this->length allowed , please help me to set the length dynamically

CodePudding user response:

one more way is there for doing the same thing

->rules('nullable',"digits:.$this->length")

I tested in my local it's working fine ,i hope it will resolve your problem.

CodePudding user response:

  1. You need the 'digits' rule to remain a string, so it should still have quotes around it.

  2. It looks like you are setting the length into a variable called $length rather than $this->length, so you need to consume it the same.

$length=25;

 Text::make('TrackID','id')
->rules('nullable','digits:'.$length)
->sortable()
->nullable()
->hideFromIndex(),

There are a couple of important concepts at play here. I encourage you to study these resources as a great way to continue your journey:

Variable scope - https://www.php.net/manual/en/language.variables.scope.php

String concatenation - https://www.php.net/manual/en/language.operators.string.php

  •  Tags:  
  • Related