1

Out of two pairs of input fields I only need one or the other. I can't get the validation right.

listing_image_url and poster_image_url should only be required if $model->listingImage is null.

Also tried using strlen($model->listingImage) == 0.

        [['listing_image_url', 'poster_image_url'], 'required', 
            'when' => function($model){

                var_dump($model->listingImage); //result is empty string '0'

                return $model->listingImage == NULL && $model->posterImage == NULL;
            },'whenClient' => "function(attribute, value) {
                  return $('#vod-listingimage').val() == '' && $('#vod-posterimage').val() == '';
            }", 'message' => 'look'
        ],

Just as above but the other way around.

[['listingImage', 'posterImage'], 'required',
                'when' => function($model) {
                    return $model->listing_image_url == NULL && $model->poster_image_url == NULL;
                },
                'whenClient' => "function(attribute, value) {

                    return $('#vod-listing_image_url').val() == '' && $('#vod-poster_image_url').val() == '';
                }", 'message' => 'hi'
            ],
Ciprian
  • 3,066
  • 9
  • 62
  • 98
  • Could you clarify your rules perhaps using a [truth table](https://en.wikipedia.org/wiki/Truth_table)? Is setting the `poster_image` and a `listing_image_url` valid? – topher Sep 01 '16 at 09:18

2 Answers2

1

You could create your own inline validator for the model validation on backend side, like this:

[['listingImage', 'posterImage'], function($attribute, $params) {
    if ($this->listingImage === null && empty($this->$attribute)) {
          $this->addError($attribute, 'Can not be blank if listingImage is null');
    }
}]

In order to also provide the client side validation you can build a custom standalone validator.

nadar
  • 1,008
  • 11
  • 15
0

I tried myself something similar and the behavior is odd indeed. But you can create a validator that checks if only one of the two fields is selected.

public function validateListingAgainstPoster($attribute, $params) 
{
    if (!empty($this->listing_image_url) && !empty($this->poster_image_url)) {
        $this->addError($attribute, 'Only one of "Listing" or "Poster" fields can be selected.');
    }
    if (empty($this->listing_image_url) && empty($this->poster_image_url)) {
        $this->addError($attribute, 'Please select one of "Listing" or "Poster Group".');
    }
}

And in your rules:

[['listing_image_url', 'poster_image_url'], 'validateListingAgainstPoster', 'skipOnEmpty' => false, 'skipOnError' => false],
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37
  • I actually got it to work. Looking at @nadar's answer, i added `===` NULL and that seems to have fixed the problem. I ll do a little more testing and get back. – Ciprian Sep 01 '16 at 09:32
  • Oh, this is great! I was trying using empty(). Please post your answer if you manage this. – Kostas Mitsarakis Sep 01 '16 at 10:30