Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yodonai's avatar

Validate only two fields on update

I'm trying to validate only two fields at the same time if any of them is updated, but it seems "ValidatesInput::validateOnly()" is only validating the first field. Below is part of my code:

Component:

public $houseNumber;
public $poleNumber;

protected $rules = [
        'houseNumber'        => 'required_without:poleNumber|integer|numeric|nullable',
        'poleNumber'       => 'required_without:houseNumber|integer|numeric|nullable'
];

public function updated($field){
        if (in_array($campo, ["houseNumber", "poleNumber"])) {
            $this->validateOnly("houseNumber");
			$this->validateOnly("poleNumber");
        } else {
			$this->validateOnly($field);
		}
}

View:

<div class="col-md-4 mt-4">
		<x-input id="houseNumber" wire:model="houseNumber" wire:key="houseNumber"
                        label="House Number*" maxlength="20" />
</div>
 <div class="col-md-4 mt-4">
		<x-input id="poleNumber" wire:model="poleNumber" wire:key="poleNumber" label="Pole Number*"
                        maxlength="20" />
</div>

What am I doing wrong?

0 likes
6 replies
yodonai's avatar

I realized I only have a problem if the first validation throws an error, as it stops and shows the error message for the "houseNumber" field only, in this case. How could I make it show for both fields without using "ValidatesInput::validate()"?

yodonai's avatar

@Snapey Mostly because my boss told me so :D

I've managed to get the result I needed with:

	public function updated($field)
    {
        if (in_array($campo, ["houseNumber", "poleNumber"])) {
            $this->withValidator(function (Validator $validator) {
                if ($validator->fails()) {
                    $this->addError("poleNumber", "Pole Number* or House Number* field is mandatory.");
                    $this->addError("houseNumber", "House Number* or Pole Number* field is mandatory.");
                }
            })->validateOnly($field);
            if ($campo == "houseNumber") {
                $this->validateOnly("poleNumber");
            } else {
                $this->validateOnly("houseNumber");
            }
        } else {
            $this->validateOnly($field);
        }
    }

But I'm not sure it's the best solution.

Snapey's avatar

what is the business requirement?

Rikaelus's avatar

@Snapey I've got one.

You have a field on the page that, based on its value, alters validation rules for other fields. So when a change is made to that field you may want to validate only the affected fields; multiple fields but not all fields.

I've run into this issue a few times now and still wish there were a validateMultiple or that validateOnly could take an array.

Rikaelus's avatar

I still need to test the hell out of it but so far this prototype trait seems to be holding up surprisingly well. It runs through each field individually and builds up a composite ValidationException to throw. The visual messages are updating accordingly without affecting those of other fields.

Please or to participate in this conversation.