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]);
    }
}
eski009
  • 371
  • 1
  • 6
  • 14
  • what are you exactly trying to do? you are checking for isset in your custom code rather you could have used required? – Trying Tobemyself Jul 03 '13 at 15:06
  • 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. – Rob W Jul 03 '13 at 15:16
  • 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. – eski009 Jul 03 '13 at 17:36

5 Answers5

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!
}
CodeBull
  • 303
  • 3
  • 7
8

In Laravel 5,

if($request->has('address2')){
  // do stuff
}
DJ Far
  • 497
  • 5
  • 12
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.

EThaiZone
  • 311
  • 1
  • 8
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
}
Trying Tobemyself
  • 3,668
  • 3
  • 28
  • 43
  • 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. – eski009 Jul 04 '13 at 08:46
  • you have to do this manually as what I have told above, isset($addr2) will return false if its empty or not sent. – Trying Tobemyself Jul 04 '13 at 13:21
  • That is a shame as I would ideally want to use Laravel validation. – eski009 Jul 08 '13 at 09:12
  • 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... – Trying Tobemyself Jul 08 '13 at 09:43
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.

Carlos Arauz
  • 805
  • 6
  • 8