Check if field exists in Input during validation using Laravel
Asked Answered
A

5

9

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]);
    }
}
Azerbaijan answered 3/7, 2013 at 14:24 Comment(3)
what are you exactly trying to do? you are checking for isset in your custom code rather you could have used required?Columbite
Do you have a specific set of values that "address2" should match? There are a lot of validation rules that come with Laravel and for something as simple as address2, I'm not sure you'd need a custom validation.Minne
I'm sorry, but I may not have explained myself properly. It is not currently possible using any existing validation rule to check that the $_POST[attribute] exists but allow an empty string as its value. I want to accept empty strings, but check that the variable 'address2' is in the post request. My attempt above to create a custom validation rule doesn't seem to work. I of course, could just check to see if it isset in the code, but I am trying to keep things clean with validation rules before performing any additional logic.Azerbaijan
O
4

You should make custom validator like this.

use Symfony\Component\Translation\TranslatorInterface;

class CustomValidator extends Illuminate\Validation\Validator {

    public function __construct(TranslatorInterface $translator, $data, $rules, $messages = array())
    {
        parent::__construct($translator, $data, $rules, $messages);

        $this->implicitRules[] = 'AttributeExists';
    }

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

This will make AttributeExists work without to use require. For more explain about this. When you want to create new validator rule. If you don't set it in $implicitRules, that method will not work out if you don't use require rule before it. You can find more info in laravel source code.

Object answered 16/9, 2013 at 7:56 Comment(0)
M
20

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!
}
Mudfish answered 26/9, 2014 at 5:11 Comment(0)
D
8

In Laravel 5,

if($request->has('address2')){
  // do stuff
}
Deborahdeborath answered 20/8, 2017 at 14:5 Comment(0)
O
4

You should make custom validator like this.

use Symfony\Component\Translation\TranslatorInterface;

class CustomValidator extends Illuminate\Validation\Validator {

    public function __construct(TranslatorInterface $translator, $data, $rules, $messages = array())
    {
        parent::__construct($translator, $data, $rules, $messages);

        $this->implicitRules[] = 'AttributeExists';
    }

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

This will make AttributeExists work without to use require. For more explain about this. When you want to create new validator rule. If you don't set it in $implicitRules, that method will not work out if you don't use require rule before it. You can find more info in laravel source code.

Object answered 16/9, 2013 at 7:56 Comment(0)
C
2

When you submit a form each and every field is posted, matter of fact is if you leave some filed empty then that field value is null or empty. Just check the POST parameters once, to do so open the firebug console in firefox and submit the form, then check the post parameters. As you want to accept empty string what is the use of any rule?

else You can do this

$addr2=Input::get('address2');
if(isset($addr2)){
 //do here whatever you want
}else{
 //do something else
 $addr2='';//empty string
}
Columbite answered 3/7, 2013 at 18:49 Comment(4)
Hi Rahul, That is not technically correct. A user can manually remove a field from the post request by editing the DOM. I'm less worried about that, but want this to be used as part of an API. If the field is not sent, I want validation to fail. If the string is sent, but is empty, it should pass.Azerbaijan
you have to do this manually as what I have told above, isset($addr2) will return false if its empty or not sent.Columbite
That is a shame as I would ideally want to use Laravel validation.Azerbaijan
Laravel Validation is working fine, But I don't think there is any such validation available or will be to make sure it is required yet empty...Columbite
R
2

Actually, Laravel has a method to validate if an attribute exists even if not filled.

$rules = [
   'something' => 'present'
];

All the validation rules are stored in Validator class (/vendor/laravel/framework/src/Illuminate/Validation/Validator.php), you can check for the implementation of each rule, even no documented rules.

Rectal answered 13/5, 2016 at 1:35 Comment(1)
Can you add the reference? except: sometimes validation rule.Citronella

© 2022 - 2024 — McMap. All rights reserved.