Check if field exists in Input during validation using Laravel

eski009 picture eski009 · Jul 3, 2013 · Viewed 28.4k times · Source

I want to make sure that certain fields are posted as part of the form but I don;t mind if some are empty values.

The 'required' validation rule won't work as I am happy to accept empty strings. I have tried the below, but as the 'address2' field is never sent, the validator doesn't process it.

Any ideas?

$rules = array(
     'address2' => 'attribute_exists'
);



class CustomValidator extends Illuminate\Validation\Validator {

    public function validateAttributeExists($attribute, $value, $parameters)
    {
        return isset($this->data[$attribute]);
    }
}

Answer

CodeBull picture CodeBull · Sep 26, 2014

You can use Input::has('address2') to check if something is posted by address2 input name. See the example:

if(Input::has('address2')) {
    // Do something!
}