Using Getters in forms
When using Get $get in forms, and you want to get the value of lets say a component that is AFTER the component that has the getter, it will return null. For example:
Is this intended? I assume the components in the form are processed linearly, but wouldnt it be better for the Getter to check all the components and not just the ones that "have already been processed"?
24 Replies
does it work if you change
Hidden
to TextInput
?No, also null
Its not a big issue, its a bit annoying 😅
What exactly are you trying to do? It’s not a good idea to use hidden fields for record id’s since they can be manipulated in the browser before form submission.
hum, I think this is the question:
this doesn't work
Maybe this is correct, the author_id isn't hydrated at this point using default, which is why it is null..
This will work
That's why i'm asking for the use case.
Well, I think this cause the
TextInput::make('author_name'
) is attempting to format its state using the value of 'author_id
'.
However, 'author_id'
is set with a default value of 1. If author_name
depends on author_id
, but there’s no mechanism to ensure author_id
is updated before the state of author_name
, this can result in incorrect or unexpected behavior.
So, I think it would be better to be initialized first.
TextInput::make(author_id)->default(1)Well, I have an author_id column in my db, on the create record, I want to have the name of the currently auth user as the default value. The default value will be an int, but I want to show the name of the user
I have done this like 100 times before, I have just completely forgotten
Sounds like it should just be a select with a relationship to the author.
Yeah I thought so too... I was just trying to think of a way to do it with a textinput
the problem with a text input is that it would require the user to know the id or the exact spelling of the user name when they select the author
you mean, this?
Thanks, I will try this when I get home
Should I make a author() relationship in the model?
Yep
@Matthew did it work?
Hey, no it didnt. Im getting that
author_id
doesnt have default value
I'd just do:
Assuming the author_id is on the same table as collaborators_id.
You have two TextInput::make('name') definitions, which may cause issues since form field names must be unique. Consider renaming the second TextInput to something descriptive like author_name:
No it won't because onces in a relationship and sub-keyed.
Well, in that case, I think it would be better to define relationship as follow.
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
You mean exactly as he has done a few posts above....
Hmmm...
How about this?
I guess it would works by reordering component places.
like this:
Hidden::make('author_id')
->label('Author')
->default(fn () => Auth::id())
->required(),
TextInput::make('author_name')
->label('Author')
->disabled()
->dehydrated()
->formatStateUsing(fn (Get $get) => dd($get('author_id')))
->required()
I don't understand what you are trying to do.. maybe you could share a repo on github reproducing the issue and what you need..
this was mentioned at the first comment..
I think he is trying to help but it should be solved already now.
I thnk this got a whole lot more complicated than necessary. I will probably just stick with Select... I reallly appreciate your help though 🙂
Good shout!