Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if field exists in Input during validation using Laravel

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]);
    }
}
like image 993
eski009 Avatar asked Jul 03 '13 14:07

eski009


1 Answers

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!
}
like image 160
CodeBull Avatar answered Oct 21 '22 14:10

CodeBull