Use radio buttons when you need users to select only one option from a small set of mutually exclusive choices. They are best for clear, concise options that don’t require scrolling or searching. Unlike dropdowns, radio buttons make all choices visible at once, reducing the need for interaction.
Select, on the other hand, is better suited for situations with many options or when saving screen space is a priority, as it hides choices in a dropdown menu.
The Radio component needs <zinq:radio.option>
elements to define the options.
Each <zinq:radio.option>
element should contain the text that will be displayed to the user and value
attribute to define the value of the option.
<zinq:radio>
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
The Radio component can be customized in many ways.
Like many other form components, the Radio component can have a label. You can set the label by using the label
attribute.
<zinq:radio label="Choose your course difficulty">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
The Radio component comes in three sizes: default
, sm
, and lg
. By default, the size is default
.
<div class="flex flex-col space-y-6">
<zinq:radio size="sm">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
<zinq:radio>
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
<zinq:radio size="lg">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
</div>
Place radio buttons on the same line by adding the inline
attribute to the <zinq:radio>
component.
<zinq:radio inline>
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
The Radio component state can be changed from any other component. You can simply use Alpine's @click
directive to toggle the state.
Just define an id for the radio and use the Alpine.store
to change the state.
<zinq:radio id="my-radio-id">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
<div class="mt-4">
<zinq:badge
@click="Alpine.store('zinq_radio')['my-radio-id'].value = 'intermediate'"
class="cursor-pointer select-none"
>I know a little bit</zinq:badge>
</div>
You can set the default value of the radio group by setting the value
attribute on the zinq:form.radio-group
component.
<zinq:radio value="intermediate">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
You can use the wire:model
directive to bind the Radio to a Livewire property.
<zinq:radio wire:model="difficulty">
...
</zinq:radio>
You can also use wire:model.live
to update the model on every change.
Course difficulty: beginner
public string $difficulty = '';
public function render(): string
{
return <<<HTML
<div>
<p>Course difficulty: {$this->difficulty}</p>
<zinq:radio wire:model.live="difficulty">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
</div>
HTML;
}
The Radio component will automatically display validation errors for you when wire:model
attribute is used.
#[Rule('required')]
public string $difficulty = '';
public function save(): void
{
$this->validate();
}
public function render(): string
{
return <<<HTML
<zinq:form action="save">
<zinq:radio wire:model="difficulty">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>
<zinq:button>Save to see validation error</zinq:button>
</zinq:form>
HTML;
}
Or you can manually display validation errors by using the error
attribute.
<zinq:radio error="We do not offer pro difficulty yet!">
<zinq:radio.option value="beginner">Beginner</zinq:form.option>
<zinq:radio.option value="intermediate">Intermediate</zinq:form.option>
<zinq:radio.option value="advanced">Advanced</zinq:form.option>
</zinq:radio>