yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

Issue with unique validator #6044

Closed ghost closed 9 years ago

ghost commented 10 years ago

I can't create an user with unique username when i'm using the UniqueValidator class. Error: after submitting the form, I'm not redirected to the view page. I see a blank page on browser, and user was not created. it doesn't work even if I enter a username that doesn't yet exist in the database.

 public function rules()
    {
        return [
            [['username', 'email', 'password'], 'required'],
            [['username', 'email'],'unique'],
            [['email'],'email'],
            [['profil_picture', 'sexe', 'show_birthday', 'show_email'], 'integer'],
            [['signup_date', 'birthday'], 'safe'],
            [['biography'], 'string'],
            [['username'], 'string', 'max' => 64],
            [['email', 'password'], 'string', 'max' => 128]
        ];
    }
samdark commented 10 years ago

What's the error? What data you're entering?

lucianobaraglia commented 10 years ago

Is not that the syntax to make username AND email unique together?

https://github.com/yiisoft/yii2/blob/master/framework/validators/UniqueValidator.php#L27

ghost commented 10 years ago

@lucianobaraglia this doesn't work!! same problem.

sorry for my english

samdark commented 10 years ago

What's in your logs?

ghost commented 10 years ago

2014-11-13 17:21:58 [127.0.0.1][-][-][info][application] $_POST = [ '_csrf' => 'dTNsTXRyNEgFfhZ/FTEBJEZXBTUeAHUKGFkDFwE8QixNViMsPTwALQ==' 'User' => [ 'username' => 'sitraka' 'email' => 'ratsimbasitraka@gmail.com' 'password' => '1234' ] ]

$_COOKIE = [ '_identity' => '854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s:49:\"[\"31\",\"5FrGIVMraKbUjcWvOsDERkMN7XGk5eyB\",2592000]\";' 'PHPSESSID' => '3ha4m1ob5kceeubrjufs98f3d3' '_csrf' => '91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes:32:\"pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e\";' ]

$_SERVER = [ 'HTTP_HOST' => 'localhost' 'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0' 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8' 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5' 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate' 'HTTP_REFERER' => 'http://localhost/basic/web/index.php/user/create' 'HTTP_COOKIE' => '_identity=854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s%3A49%3A%22%5B%2231%22%2C%225FrGIVMraKbUjcWvOsDERkMN7XGk5eyB%22%2C2592000%5D%22%3B; PHPSESSID=3ha4m1ob5kceeubrjufs98f3d3; _csrf=91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes%3A32%3A%22pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e%22%3B' 'HTTP_CONNECTION' => 'keep-alive' 'HTTP_CACHE_CONTROL' => 'max-age=0' 'CONTENT_TYPE' => 'application/x-www-form-urlencoded' 'CONTENT_LENGTH' => '163' 'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' 'SERVER_SIGNATURE' => ' Apache/2.4.7 (Ubuntu) Server at localhost Port 80 ' 'SERVER_SOFTWARE' => 'Apache/2.4.7 (Ubuntu)' 'SERVER_NAME' => 'localhost' 'SERVER_ADDR' => '127.0.0.1' 'SERVER_PORT' => '80' 'REMOTE_ADDR' => '127.0.0.1' 'DOCUMENT_ROOT' => '/var/www' 'REQUEST_SCHEME' => 'http' 'CONTEXT_PREFIX' => '' 'CONTEXT_DOCUMENT_ROOT' => '/var/www' 'SERVER_ADMIN' => '[no address given]' 'SCRIPT_FILENAME' => '/var/www/basic/web/index.php' 'REMOTE_PORT' => '48051' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'SERVER_PROTOCOL' => 'HTTP/1.1' 'REQUEST_METHOD' => 'POST' 'QUERY_STRING' => '' 'REQUEST_URI' => '/basic/web/index.php/user/create' 'SCRIPT_NAME' => '/basic/web/index.php' 'PATH_INFO' => '/user/create' 'PATH_TRANSLATED' => '/var/www/user/create' 'PHP_SELF' => '/basic/web/index.php/user/create' 'REQUEST_TIME_FLOAT' => 1415899318.449 'REQUEST_TIME' => 1415899318 ]

samdark commented 10 years ago

I meant logs, not request info.

ghost commented 10 years ago

2014-11-13 17:16:58 [127.0.0.1][-][-][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: app\models\User::username, email' in /var/www/basic/vendor/yiisoft/yii2/base/Component.php:143 Stack trace:

0 /var/www/basic/vendor/yiisoft/yii2/db/BaseActiveRecord.php(246): yii\base\Component->__get('username, email')

1 /var/www/basic/models/User.php(57): yii\db\BaseActiveRecord->__get('username, email')

2 [internal function]: app\models\User->validateUE('username, email', NULL)

3 /var/www/basic/vendor/yiisoft/yii2/validators/InlineValidator.php(67): call_user_func(Array, 'username, email', NULL)

4 /var/www/basic/vendor/yiisoft/yii2/validators/Validator.php(238): yii\validators\InlineValidator->validateAttribute(Object(app\models\User), 'username, email')

5 /var/www/basic/vendor/yiisoft/yii2/base/Model.php(333): yii\validators\Validator->validateAttributes(Object(app\models\User), Array)

6 /var/www/basic/vendor/yiisoft/yii2/db/ActiveRecord.php(408): yii\base\Model->validate(NULL)

7 /var/www/basic/vendor/yiisoft/yii2/db/BaseActiveRecord.php(582): yii\db\ActiveRecord->insert(true, NULL)

8 /var/www/basic/controllers/UserController.php(68): yii\db\BaseActiveRecord->save()

9 [internal function]: app\controllers\UserController->actionCreate()

10 /var/www/basic/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)

11 /var/www/basic/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)

12 /var/www/basic/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('create', Array)

13 /var/www/basic/vendor/yiisoft/yii2/web/Application.php(83): yii\base\Module->runAction('user/create', Array)

14 /var/www/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))

15 /var/www/basic/web/index.php(12): yii\base\Application->run()

16 {main}

2014-11-13 17:16:57 [127.0.0.1][-][-][info][application] $_POST = [ '_csrf' => 'YjR1VlFtN3ESeQ9kMC4CHVFQHC47H3YzD14aDCQjQRVaUTo3GCMDFA==' 'User' => [ 'username' => 'sitraka' 'email' => 'ratsimbasitraka@gmail.com' 'password' => '1234' ] ]

$_COOKIE = [ '_identity' => '854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s:49:\"[\"31\",\"5FrGIVMraKbUjcWvOsDERkMN7XGk5eyB\",2592000]\";' 'PHPSESSID' => '3ha4m1ob5kceeubrjufs98f3d3' '_csrf' => '91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes:32:\"pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e\";' ]

$_SERVER = [ 'HTTP_HOST' => 'localhost' 'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0' 'HTTPACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,/_;q=0.8' 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5' 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate' 'HTTP_REFERER' => 'http://localhost/basic/web/index.php/user/create' 'HTTP_COOKIE' => '_identity=854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s%3A49%3A%22%5B%2231%22%2C%225FrGIVMraKbUjcWvOsDERkMN7XGk5eyB%22%2C2592000%5D%22%3B; PHPSESSID=3ha4m1ob5kceeubrjufs98f3d3; _csrf=91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes%3A32%3A%22pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e%22%3B' 'HTTP_CONNECTION' => 'keep-alive' 'HTTP_CACHE_CONTROL' => 'max-age=0' 'CONTENT_TYPE' => 'application/x-www-form-urlencoded' 'CONTENT_LENGTH' => '161' 'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' 'SERVER_SIGNATURE' => '

Apache/2.4.7 (Ubuntu) Server at localhost Port 80
' 'SERVER_SOFTWARE' => 'Apache/2.4.7 (Ubuntu)' 'SERVER_NAME' => 'localhost' 'SERVER_ADDR' => '127.0.0.1' 'SERVER_PORT' => '80' 'REMOTE_ADDR' => '127.0.0.1' 'DOCUMENT_ROOT' => '/var/www' 'REQUEST_SCHEME' => 'http' 'CONTEXT_PREFIX' => '' 'CONTEXT_DOCUMENT_ROOT' => '/var/www' 'SERVER_ADMIN' => '[no address given]' 'SCRIPT_FILENAME' => '/var/www/basic/web/index.php' 'REMOTE_PORT' => '47899' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'SERVER_PROTOCOL' => 'HTTP/1.1' 'REQUEST_METHOD' => 'POST' 'QUERY_STRING' => '' 'REQUEST_URI' => '/basic/web/index.php/user/create' 'SCRIPT_NAME' => '/basic/web/index.php' 'PATH_INFO' => '/user/create' 'PATH_TRANSLATED' => '/var/www/user/create' 'PHP_SELF' => '/basic/web/index.php/user/create' 'REQUEST_TIME_FLOAT' => 1415899017.095 'REQUEST_TIME' => 1415899017 ] 2014-11-13 17:21:59 [127.0.0.1][-][-][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: app\models\User::a1' in /var/www/basic/vendor/yiisoft/yii2/base/Component.php:143 Stack trace:

0 /var/www/basic/vendor/yiisoft/yii2/db/BaseActiveRecord.php(246): yii\base\Component->__get('a1')

1 /var/www/basic/vendor/yiisoft/yii2/validators/UniqueValidator.php(86): yii\db\BaseActiveRecord->__get('a1')

2 /var/www/basic/vendor/yiisoft/yii2/validators/Validator.php(238): yii\validators\UniqueValidator->validateAttribute(Object(app\models\User), 'username')

3 /var/www/basic/vendor/yiisoft/yii2/base/Model.php(333): yii\validators\Validator->validateAttributes(Object(app\models\User), Array)

4 /var/www/basic/vendor/yiisoft/yii2/db/ActiveRecord.php(408): yii\base\Model->validate(NULL)

5 /var/www/basic/vendor/yiisoft/yii2/db/BaseActiveRecord.php(582): yii\db\ActiveRecord->insert(true, NULL)

6 /var/www/basic/controllers/UserController.php(68): yii\db\BaseActiveRecord->save()

7 [internal function]: app\controllers\UserController->actionCreate()

8 /var/www/basic/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)

9 /var/www/basic/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)

10 /var/www/basic/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('create', Array)

11 /var/www/basic/vendor/yiisoft/yii2/web/Application.php(83): yii\base\Module->runAction('user/create', Array)

12 /var/www/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))

13 /var/www/basic/web/index.php(12): yii\base\Application->run()

14 {main}

2014-11-13 17:21:58 [127.0.0.1][-][-][info][application] $_POST = [ '_csrf' => 'dTNsTXRyNEgFfhZ/FTEBJEZXBTUeAHUKGFkDFwE8QixNViMsPTwALQ==' 'User' => [ 'username' => 'sitraka' 'email' => 'ratsimbasitraka@gmail.com' 'password' => '1234' ] ]

$_COOKIE = [ '_identity' => '854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s:49:\"[\"31\",\"5FrGIVMraKbUjcWvOsDERkMN7XGk5eyB\",2592000]\";' 'PHPSESSID' => '3ha4m1ob5kceeubrjufs98f3d3' '_csrf' => '91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes:32:\"pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e\";' ]

$_SERVER = [ 'HTTP_HOST' => 'localhost' 'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0' 'HTTPACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,/_;q=0.8' 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5' 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate' 'HTTP_REFERER' => 'http://localhost/basic/web/index.php/user/create' 'HTTP_COOKIE' => '_identity=854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s%3A49%3A%22%5B%2231%22%2C%225FrGIVMraKbUjcWvOsDERkMN7XGk5eyB%22%2C2592000%5D%22%3B; PHPSESSID=3ha4m1ob5kceeubrjufs98f3d3; _csrf=91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes%3A32%3A%22pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e%22%3B' 'HTTP_CONNECTION' => 'keep-alive' 'HTTP_CACHE_CONTROL' => 'max-age=0' 'CONTENT_TYPE' => 'application/x-www-form-urlencoded' 'CONTENT_LENGTH' => '163' 'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' 'SERVER_SIGNATURE' => '

Apache/2.4.7 (Ubuntu) Server at localhost Port 80
' 'SERVER_SOFTWARE' => 'Apache/2.4.7 (Ubuntu)' 'SERVER_NAME' => 'localhost' 'SERVER_ADDR' => '127.0.0.1' 'SERVER_PORT' => '80' 'REMOTE_ADDR' => '127.0.0.1' 'DOCUMENT_ROOT' => '/var/www' 'REQUEST_SCHEME' => 'http' 'CONTEXT_PREFIX' => '' 'CONTEXT_DOCUMENT_ROOT' => '/var/www' 'SERVER_ADMIN' => '[no address given]' 'SCRIPT_FILENAME' => '/var/www/basic/web/index.php' 'REMOTE_PORT' => '48051' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'SERVER_PROTOCOL' => 'HTTP/1.1' 'REQUEST_METHOD' => 'POST' 'QUERY_STRING' => '' 'REQUEST_URI' => '/basic/web/index.php/user/create' 'SCRIPT_NAME' => '/basic/web/index.php' 'PATH_INFO' => '/user/create' 'PATH_TRANSLATED' => '/var/www/user/create' 'PHP_SELF' => '/basic/web/index.php/user/create' 'REQUEST_TIME_FLOAT' => 1415899318.449 'REQUEST_TIME' => 1415899318 ] 2014-11-13 20:43:12 [127.0.0.1][31][-][error][yii\web\HttpException:404] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "CREATE".' in /var/www/basic/vendor/yiisoft/yii2/base/Module.php:461 Stack trace:

0 /var/www/basic/vendor/yiisoft/yii2/web/Application.php(83): yii\base\Module->runAction('CREATE', Array)

1 /var/www/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))

2 /var/www/basic/web/index.php(12): yii\base\Application->run()

3 {main}

Next exception 'yii\web\NotFoundHttpException' with message 'Unable to resolve the request "CREATE".' in /var/www/basic/vendor/yiisoft/yii2/web/Application.php:95 Stack trace:

0 /var/www/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))

1 /var/www/basic/web/index.php(12): yii\base\Application->run()

2 {main}

2014-11-13 20:43:12 [127.0.0.1][31][-][info][application] $_COOKIE = [ '_identity' => '854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s:49:\"[\"31\",\"5FrGIVMraKbUjcWvOsDERkMN7XGk5eyB\",2592000]\";' 'PHPSESSID' => '7vt6vv6eu66gdf4t309am1ljf4' '_csrf' => '91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes:32:\"pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e\";' ]

$_SESSION = [ 'flash' => [] 'id' => '31' ]

$_SERVER = [ 'HTTP_HOST' => 'localhost' 'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0' 'HTTPACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,/_;q=0.8' 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5' 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate' 'HTTP_COOKIE' => '_identity=854a02187bab9b937e5520c7eab2009ded817fd5b82286e6b46c98798cb33310s%3A49%3A%22%5B%2231%22%2C%225FrGIVMraKbUjcWvOsDERkMN7XGk5eyB%22%2C2592000%5D%22%3B; PHPSESSID=7vt6vv6eu66gdf4t309am1ljf4; _csrf=91e984806b726594a75ab37b13dd3a1510254e7717153d627098f57649ad8faes%3A32%3A%22pMz2aC5l3dixjrABmjoZuNvd8eOaIN4e%22%3B' 'HTTP_CONNECTION' => 'keep-alive' 'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' 'SERVER_SIGNATURE' => '

Apache/2.4.7 (Ubuntu) Server at localhost Port 80
' 'SERVER_SOFTWARE' => 'Apache/2.4.7 (Ubuntu)' 'SERVER_NAME' => 'localhost' 'SERVER_ADDR' => '127.0.0.1' 'SERVER_PORT' => '80' 'REMOTE_ADDR' => '127.0.0.1' 'DOCUMENT_ROOT' => '/var/www' 'REQUEST_SCHEME' => 'http' 'CONTEXT_PREFIX' => '' 'CONTEXT_DOCUMENT_ROOT' => '/var/www' 'SERVER_ADMIN' => '[no address given]' 'SCRIPT_FILENAME' => '/var/www/basic/web/index.php' 'REMOTE_PORT' => '49440' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'SERVER_PROTOCOL' => 'HTTP/1.1' 'REQUEST_METHOD' => 'GET' 'QUERY_STRING' => '' 'REQUEST_URI' => '/basic/web/index.php/CREATE' 'SCRIPT_NAME' => '/basic/web/index.php' 'PATH_INFO' => '/CREATE' 'PATH_TRANSLATED' => '/var/www/CREATE' 'PHP_SELF' => '/basic/web/index.php/CREATE' 'REQUEST_TIME_FLOAT' => 1415911392.861 'REQUEST_TIME' => 1415911392 ]

lynicidn commented 10 years ago

check your code u have [['username, email'], 'unique'] need [['username', 'email'], 'unique']

ghost commented 10 years ago

rules look like this:

public function rules()
    {
        return [
            [['username', 'email', 'password'], 'required'],
            [['username','email'], 'unique', 'targetAttribute' => ['username', 'email']],
            //['username', 'validateUsername', 'skipOnEmpty' => false, 'skipOnError' => false],
            [['email'],'email'],
            [['profil_picture', 'sexe', 'show_birthday', 'show_email'], 'integer'],
            [['signup_date', 'birthday'], 'safe'],
            [['biography'], 'string'],
            [['username'], 'string', 'max' => 64],
            [['email', 'password'], 'string', 'max' => 128]
        ];
    }
ghost commented 10 years ago

when I fill fields, errors are not displayed. yet, username and email exist in the database. screenshot from 2014-11-14 01 14 58

cebe commented 10 years ago

have you clicked "Create"? client validation can not check for uniqueness. this is done on the server. you need to enable ajax validation if you want it directly.

qiangxue commented 10 years ago

The validation you saw is done on the client side which doesn't include the unique validation unless you configure it to be done via AJAX.

nenad-zivkovic commented 10 years ago

sitchaka try this:

public function rules()
{
    return [
        [['username', 'email', 'password'], 'required'],
        ['username', 'unique', 'targetClass' => '\common\models\User', 
            'message' => 'This username has already been taken.'],
        ['email', 'unique', 'targetClass' => '\common\models\User', 
            'message' => 'This email address has already been taken.'],
        [['email'],'email'],
        [['profil_picture', 'sexe', 'show_birthday', 'show_email'], 'integer'],
        [['signup_date', 'birthday'], 'safe'],
        [['biography'], 'string'],
        [['username'], 'string', 'max' => 64],
        [['email', 'password'], 'string', 'max' => 128]
    ];
}
klimov-paul commented 9 years ago

Closed unless more information arrives.

wwebdevelop commented 9 years ago

I'm having the same problem .

See the problem context .

It occurs when I set the validator "unique " , " exist" or even when I do a custom function to do the validation .

When I submit the form, simply the page is blank. There is no error fired by Yii or browser console.

This same problem is occurring in both systems I'm developing in Yii . A MySQL , another for Oracle.

Follows the code :

public function rules() { return [ [['IDEXERCICIO', 'IDMES', 'IDORIGEMRECEITA', 'VALOR'], 'required'], [['IDEXERCICIO', 'IDMES', 'IDORIGEMRECEITA'], 'integer'],
[['VALOR'], 'double', 'integerOnly'=>false, 'numberPattern'=>'/^\s(?:[1-9]\d{0,2}(?:.\d{3})|0)(?:,\d{1,2})?$/'], [['OBSERVACAO'], 'safe'], [['IDORIGEMRECEITA'], 'unicaOrigem'], //[['IDORIGEMRECEITA'], 'unique', 'filter'=>'ATIVO=1'], //[['IDORIGEMRECEITA'], 'exist'], ]; }

//-------------------------------------------------------------------------------------

public function unicaOrigem($attribute, $params) { $origem = self::find()->where( "IDORIGEMRECEITA = $this->IDORIGEMRECEITA AND ATIVO= 1")->one();

     if($origem != NULL)
     {
         $this->addError('IDORIGEMRECEITA', 'record already exist' );
     }        
}

//----------------------------------------------------------------------------------------

Controller:

public function actionCreate() { $model = new RECEITA();

    if($model->load(Yii::$app->request->post()))
    {            
        $model->IDUSUARIO = Yii::$app->user->identity->IDUSUARIO;            
        $model->DATASISTEMA = Util::geraData();                

        if ($model->save()) 
        {                 
            return $this->redirect(['view', 'id' => $model->IDRECEITA]);
        }
    }
    else 
    {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

I've tried the three forms and it still fails

cebe commented 9 years ago

the problem in your case is controller logic: you have one case that exists the action without printing anything.

change it to the following should work, this way you catch the case of failing validation by displaying the form with errors:

public function actionCreate()
{
    $model = new RECEITA();

    if($model->load(Yii::$app->request->post()))
    {            
        $model->IDUSUARIO = Yii::$app->user->identity->IDUSUARIO;            
        $model->DATASISTEMA = Util::geraData();                

        if ($model->save()) 
        {                 
            return $this->redirect(['view', 'id' => $model->IDRECEITA]);
        }
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}
wwebdevelop commented 9 years ago

Cebe, thank you for your answer. You are absolutely right.

Indeed, if the save () fails is not rendered anything;

I changed the code generated via CRUD Gii and not hearken to me that speaks of semantics.

Many thanks again.

MilinkoDragovic commented 8 years ago

Hello guys,

I'm trying to make unique mobile number on registration. So, I'm using Ajax validation. But I got blank page when I click on button save. Can you help me :)

Model:

  public function rules()
    {
        return [
            [['clicnic_idclicnic'], 'required'],
            [['clicnic_idclicnic','day','month','year'], 'integer'],
            [['birthday', 'first_login_date', 'last_login_date'], 'safe'],
            [['first_name', 'last_name', 'street', 'plc', 'city', 'phone', 'mobile', 'email'], 'string', 'max' => 45],
            [['mobile'],'unique'],

        ];
    }

View

 <?php $form = ActiveForm::begin([
        'enableAjaxValidation'=>true,
    ]); ?>
.....
<?= $form->field($model, 'mobile')->textInput(['maxlength' => true]) ?>

Controller

  public function actionCreate()
    {
        $model = new Patient();

        if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
            Yii::$app->response->format = 'json';
            return ActiveForm::validate($model);
        }
        $year[] = null;
        for ($i=1950; $i <= date("Y"); $i++) { 
            $year[$i] = $i;
        }
        $day[] = null;
        for ($i=1; $i <= 31; $i++) { 
            $day[$i] = $i;
        }
        $month[] = null;
        for ($i=1; $i <= 12; $i++) { 
            $month[$i] = $i;
        }
        if ($model->load(Yii::$app->request->post()) ) {
            $model->birthday = $model->year.'-'.$model->month.'-'.$model->day;
            if ($model->save()) {
                return $this->redirect(['appointment/index','phonenum'=>$model->mobile]);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
                'clinic' => Clinic::find()->all(),
                'day' => $day,
                'month' => $month,
                'year' => $year,
            ]);
        }
    }
SilverFire commented 8 years ago

What is the status-code of that blank page?

yii-bot commented 8 years ago

This is an automated comment, triggered by adding the label question.

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem. If the result of the discussion turns out that there really is a bug in the framework, feel free to come back and provide information on how to reproduce the issue. This issue will be closed for now.

MilinkoDragovic commented 8 years ago

Redirect to this link: http://localhost:8080/klijent/web/index.php?r=patient%2Fcreate And Its blank. Confirm Form Resubmission "This webpage requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. "

SilverFire commented 8 years ago

I think you are getting a blank page when save() failed. Check you controller's action code, there is no logical branch with return, when $model->save() is false.

          if ($model->load(Yii::$app->request->post()) ) {
             $model->birthday = $model->year.'-'.$model->month.'-'.$model->day;
             if ($model->save()) {
                 return $this->redirect(['appointment/index','phonenum'=>$model->mobile]);
             }
         } else {