Jump to content



Photo

Instant Validation With Jquery $.Post


  • Please log in to reply
8 replies to this topic

#1 Julia

Julia

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 3 May 2012 - 2:55 AM

Hi,

I am having difficulty to have an instant validation carried out immediately after input of a value in a view file.

Here is the code (extract of the view file) that calls for the function (found in the controller file)

View file:

<?php Yii::app()->getClientScript()->registerScript('checkid2','$("#thisid").change(function()
{
alert("actiondupecheck");
$.post("index.php?r=roomtype/dupecheck", {thisid: $("#thisid").val()}, function(data) {
$("#dupecheck").html(data);
});
}
);');
?>


Controller file:

public function actionDupecheck()
{
$thisid = trim($_POST['thisid']);
$test =Piece::model()->find("id='$thisid'");

if(!empty($test)) {
echo sprintf("This id <strong>%s</strong>, exists already.", $thisid);
}

}

I have tried several ways but am completely stuck with this. I thought there was a problem with the syntax with the URL but after replacing 'dupecheck' with 'admin' in the view file, the admin function was called. What I am trying to do with this code, is to check for a duplicate entry in my table. I have been advised to use model validation rules. I will look into this suggestion but I also want to know that is wrong with my code. Thanks
  • 0

#2 Antonio Conte

Antonio Conte

    Advanced Member

  • Members
  • PipPipPip
  • 1,064 posts
  • LocationOslo, Norway

Posted 3 May 2012 - 5:33 AM

jQuery is a JavaScript library that is client side. PHP and the $_POST array are server side. jQuery cannot validate the post array, that you'll have to do with PHP.

jQuery can grab IDs, classes and elements from a html page, thought. $(#id), $(.class), $(selector)
  • 0

#3 Julia

Julia

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 3 May 2012 - 10:34 AM

$.post initiates a POST request to the server...
  • 0

#4 Antonio Conte

Antonio Conte

    Advanced Member

  • Members
  • PipPipPip
  • 1,064 posts
  • LocationOslo, Norway

Posted 3 May 2012 - 1:45 PM

Ah, sorry then. I'm no expert when it comes to jQuery. As I've never seen that before, I just thought you'd misunderstood something. Sorry. :)
  • 0

#5 Julia

Julia

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 3 May 2012 - 1:55 PM

You could be right though because I have been struggling with this code and can't get it to work :( According to my Jquery reference book, $.post is supposed to do that. Waiting for some help from this forum
  • 0

#6 Edward

Edward

    Advanced Member

  • Members
  • PipPipPip
  • 1,128 posts

Posted 4 May 2012 - 2:04 AM

If you have not done so already, it may worth posting here http://www.yiiframework.com/forum/.

There are occasion bugs in frameworks, so it would be worth asking about this on the yii forum also to see if anyone can help you.
  • 0

#7 Edward

Edward

    Advanced Member

  • Members
  • PipPipPip
  • 1,128 posts

Posted 4 May 2012 - 2:17 AM

Hi,

I am having difficulty to have an instant validation carried out immediately after input of a value in a view file.

Here is the code (extract of the view file) that calls for the function (found in the controller file)

View file:

<?php Yii::app()->getClientScript()->registerScript('checkid2','$("#thisid").change(function()
{
alert("actiondupecheck");
$.post("index.php?r=roomtype/dupecheck", {thisid: $("#thisid").val()}, function(data) {
$("#dupecheck").html(data);
});
}
);');
?>


Controller file:

public function actionDupecheck()
{
$thisid = trim($_POST['thisid']);
$test =Piece::model()->find("id='$thisid'");

if(!empty($test)) {
echo sprintf("This id <strong>%s</strong>, exists already.", $thisid);
}

}

I have tried several ways but am completely stuck with this. I thought there was a problem with the syntax with the URL but after replacing 'dupecheck' with 'admin' in the view file, the admin function was called. What I am trying to do with this code, is to check for a duplicate entry in my table. I have been advised to use model validation rules. I will look into this suggestion but I also want to know that is wrong with my code. Thanks


Actually i think i know the solution to this problem.

Okay you need to access your User Model the ActiveRecord, then just modify the rules, i will show some sample code where i have done this before:

public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('email, username, password', 'required'),
array('email, username, password', 'length', 'max'=>256),
array('email, username', 'unique'),
array('password', 'compare'),
array('password_repeat', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, email, username, password last_login_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
);
}

So you see the part in the code array('email, username', 'unique') email and username have been declared as unique therefore only one email and username can occur in the model.
  • 0

#8 Julia

Julia

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 4 May 2012 - 12:10 PM

Thanks Edward for your suggestion. I have learnt something that I did not know even though it is not the solution to the problem. I sought the help of some people expert at PHP who found the solution to the problem. I had to modify the access rules of controller to give access to the required action that was being called in the URL parameter of Jquery $.post:

array('allow', // allow authenticated user to perform 'dupecheck' actions
'actions'=>array('dupecheck'),
'users'=> array('authorised username'),


That was a struggle!!
  • 2

#9 Edward

Edward

    Advanced Member

  • Members
  • PipPipPip
  • 1,128 posts

Posted 5 May 2012 - 2:45 AM

Glad to hear you found the solution, and thanks for posting it up here for us. :)
  • 0