0
  ['state', 'required', 'when' => function ($model) {
                return $model->country != null;
            }, 'enableClientValidation' => false],

Why does country attribute is updated to have null value when I added this code? I figured out that my rules above is not working because country is somewhere set to NULL ONLY when I add those code in my model rules.

on my controller I set data using this :

 $model = Country::findOne($country_id);
 $model->load(Yii::$app->request->post());

On my view I only have this customized html, Im not using the view of yii2

 <form ='form1'>
    <input name="Country[country_name]" type="text"/>
    </form>

<form ='form2'>
<input name="Country[state_name]" type="text"/>
</form>
4
  • Please post more of your code such as how you are loading in the user data into the model properties. Commented Apr 16, 2017 at 14:51
  • @Brett I have this part of code $model = Country::findOne($country_id); $model->load(Yii::$app->request->post()); Commented Apr 16, 2017 at 15:03
  • What happens if you var_dump(Yii::$app->request->post()) ? Commented Apr 16, 2017 at 15:36
  • For 1, the rule you have says that state is only required when country is not null. 2, I am not sure if enableClientValidation works on the rules? It goes on your ActiveForm widget in your view. If you need to toggle it, use jQuery and toggle it based on a field on your form. 3, those rules you defined don't trigger until you run $model->validate()... We need to see more of your code. The view, controller, and models related. Commented Apr 16, 2017 at 22:23

1 Answer 1

1

Since you didn't post enough code for anyone to tell what you have, this is as close as I can get. I may update this answer if you add more code. We need to see more of your model, controller, and view.

In the view file, it would help so I can see what ID's are being assigned to your inputs, which would allow me to provide more detailed code. So for right now, you need to look at your form and look at the ID's on the input elements. Replace REPLACEHERE in the javascript below with whatever your ID's start with.

model

['state_name', 'required', 'when' => function($model) {
        return !empty($model->country_name);
    }, 'whenClient' => "isCountryEmpty"
],

place at the bottom of your view file:

<?php $this->registerJs('
    function isCountryEmpty (attribute, value) {
        return ($("#REPLACEHERE-country_name").val().length > 0) ? true : false;
    };
    jQuery( "#REPLACEHERE-country_name" ).keyup(function() {
        $("#w0").yiiActiveForm("validateAttribute", "REPLACEHERE-state_name");
        $("#w0").yiiActiveForm("validateAttribute", "REPLACEHERE-country_name");
    });
    jQuery( "#REPLACEHERE-state_name" ).keyup(function() {
        $("#w0").yiiActiveForm("validateAttribute", "REPLACEHERE-state_name");
        $("#w0").yiiActiveForm("validateAttribute", "REPLACEHERE-country_name");
    });
'); ?>

The whenClient in the model tells it to run client validation and calls the isStateEmpty function in the javascript. The other 2 javascripts detect changes on the state and country inputs. On keyup they trigger the Yii ActiveForm javascript to re-validate.

Hope that helps you out!


Edit

After you added your view's code, your names are incorrect. In your view, you have state_name and country_name but in your model you used only state. I have updated my code above to reflect that.

If your not using the Yii form, you will not get client validations. Those are bound by specialized Yii ID's attached to the elements when ActiveForm creates your form.

Sign up to request clarification or add additional context in comments.

2 Comments

Please see added details on my op, I m wondering If I can bypass the whenClient vaildation ? and only when will be enough
I updated the code in my answer. You used state in your model instead of state_name as you have in your form... So of course it never runs, it's looking for the wrong element name. You also don't get client validation when you don't use the Yii ActiveForm builder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.