Reputation: 401
I have form build with yii framework. The first 2 inputs is "first_name" and "last_name" and I have many more inputs in the same form. I'm checking validation with "ajaxSubmitButton" and I want to check first only "first_name" and "last_name" pass the validation and If they do next time when user will submit its would check all form.
Model -> rules
array('first_name, last_name', 'length', 'min'=>2 , 'on' => 'setup'),
array('first_name, last_name , email, subject , message', 'length', 'min'=>2 , 'on' => 'submit'),
Ajax Button on View ->
<?php echo CHtml::ajaxSubmitButton( 'Submit',
CHtml::normalizeUrl(array('SubmitForm')),
array(
'error'=>'js:function(){
alert(\'error\');
}',
'beforeSend'=>'js:function(){
alert(\'beforeSend\');
}',
'success'=>'js:function(data){
alert(data);
}',
'complete'=>'js:function(){
// alert(\'complete\');
}',
'update'=>'#updatebox',
) ,array('id'=>'submit')
);
?>
Controller >
public function actionMessageForm()
{
$model = new Message;
if(isset($_POST['Message']))
{
$model->attributes=$_POST['Message'];
$valid = $model->validate('setup');
if($valid)
{
echo "pass";
$model->save();
}
else
{
echo "failed";
}
}
}
How can I check partly inputs? What I'm doing Wrong?
Upvotes: 0
Views: 3464
Reputation: 48256
use scenarios bro
http://www.yiiframework.com/wiki/266/understanding-scenarios/
make one scenario 'buttafuoco' (call it what you will) where only the first and last name are required
if that validates, switch the scenario to 'blagojevich' (a new scenario) where all fields are required.
you might have to store the current scenario name in session or a post variable
Upvotes: 2
Reputation: 5955
You have a bug in your controller. This:
public function actionMessageForm()
{
$model = new Message;
if(isset($_POST['Message']))
{
$model->attributes=$_POST['Message'];
$valid = $model->validate('setup');
should be this:
public function actionMessageForm()
{
$model = new Message;
if(isset($_POST['Message']))
{
$model->scenario = 'setup'; <-- changed
$model->attributes=$_POST['Message'];
$valid = $model->validate(); <-- passing a value in here is passing an attribute
or this:
public function actionMessageForm()
{
if(isset($_POST['Message']))
{
$model = new Message('setup');
Does that help?
Upvotes: 1