Yii Framework Forum: jQuery and Required form fields - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

jQuery and Required form fields Rate Topic: ***** 1 Votes

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 15 July 2010 - 09:36 AM

I use a jQuery show/hide effect to show/hide form fields based on what radio option is selected.

For example:

<label for="car">Car</label>
<input name="vehicle_type" id="car" type="radio" value="Car" />

<label for="boat">Boat</label>
<input name="vehicle_type" id="boat" type="radio" value="Boat" />

<div id="car_make_div" style="display:none">
<input type="text" id="car_make" name="car_make" />
</div>

<script type="text/javascript">
$('input[name=\'vehicle_type\']').change(function(){
if($('input[name=\'vehicle_type\']:checked').val() == 'Car')
{
	$('#car_make_div').show();
}
if($('input[name=\'vehicle_type\']:checked').val() == 'Boat')
{
	$('#car_make_div').hide();
}
</script>


In my example, the input field 'car_make' is a required field in the model. How can I make it be "required" only if is being displayed?
0

#2 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 15 July 2010 - 03:19 PM

Anyone able to advise?
0

#3 User is offline   GDzyne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-June 09

Posted 15 July 2010 - 04:10 PM

You should use scenarios in your model. view http://www.yiiframew...ring-validation

In your controller you should check which radio option is selected and then set the appropriate scenario when creating the model.
0

#4 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 15 July 2010 - 04:18 PM

Hi GDzyne,

Basically I use jQuery to show/hide fields on the page. The controller will not know which radio option has been selected.. do you mean I should send an ajax call to the controller to change the scenario?
0

#5 User is offline   GDzyne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-June 09

Posted 15 July 2010 - 04:29 PM

View PostGSTAR, on 15 July 2010 - 04:18 PM, said:

Hi GDzyne,

Basically I use jQuery to show/hide fields on the page. The controller will not know which radio option has been selected.. do you mean I should send an ajax call to the controller to change the scenario?


Hi GSTAR,

If I understand correctly you will be submitting the form to be processed (saved to DB) ?

If so, then in your action method of your controller you will need to check which option is checked.


In controller :

...
$scenario=$_POST['vehicle_type'];
$model=new Model($scenario);
// rest of assignment
...

In model :

...
public function rules()
{
    return array(
        array('car_make', 'required', 'on'=>'Car'),
        array('boat_make', 'required', 'on'=>'Boat'),
    );
}
...


1

#6 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 07:19 AM

Hi Gdzyne,

Thanks for that. However the problem with that now is upon first page load those fields are not rendered as required, i.e. we don't have "label class=required". So the user wil not know that the field is required until after the form has been submitted and they are given the error.

Any suggestion on how to rectify this?
0

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 16 July 2010 - 07:37 AM

You can manually set in htmlOptions the class required for the label.
0

#8 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 11:36 AM

How can you check multiple scenarios? For example this is what I have done in my controller action, not sure if it is correct:

if(isset($_POST['vehicle_type']))
{
	$scenario=$_POST['vehicle_type'];
	$model=new Vehicle($scenario);
}
else
{
	$model=new Vehicle;
}


The above code does a check on the vehicle type field, what if I also need to do a check on another field?
0

#9 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 16 July 2010 - 03:29 PM

I'd suggest reading through the documentation and trying stuff out, once you understand how these work it will make things much easier.... http://www.yiiframew...ring-validation


You can have a 100 (or as many as you want) seniores all with different fields or variation of fields.

$model=new Vehicle('Car');
$mode->attributes = $_POST['car'];
$model->validate();

//you can check multiple scenarios by changing it and running the validate method
$mode->setScenario("ANOTHER_ scenario");
$mode->validate();
//though you could run into problems if you don't fully understand how these work

//you can also individually validate fields if need be
$model->validate("car_make");

//rules
public function rules()
{
    return array(
        array('car_make', 'required', 'on'=>'Car'),
        array('boat_make', 'required', 'on'=>'Boat'),
        array('can_fly', 'require', 'on' => "Car, Airplane, UFO"),
        array('price', 'require', 'on' => "Car, UFO"),
    );
}



The best way is probably to just use rules! You can target multiple fields by using the ON option;

e.g. the can_fly field will only be required if the scenario is Car, Airplane, UFO but the price will only be required if the scenario is CAR or UFO.
0

#10 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 05:52 PM

Cheers Sniper ;)

I have my rules defined to perform the checks, but I'm having difficulty in checking the scenarios in my controller. This is what I have now:

Rules:

array('car_make', 'required', 'on'=>'Car'),
			
array('return_datetime', 'required', 'on'=>'Home'),

Controller:

public function actionForm()
{
	$model=new Enquiry;
	
	if(isset($_POST['vehicle_type']))
	{
		$scenario=$_POST['vehicle_type'];
		$model->setScenario($scenario);
	}
	if(isset($_POST['journey_from']))
	{
		$scenario=$_POST['journey_from'];
		$model->setScenario($scenario);
	}
	
	if(isset($_POST['Enquiry']))
	{
		$model->attributes=$_POST['Enquiry'];
			
		if($model->validate())
		{
			// processing here
		}
	}
	
	$this->render('index', array('model'=>$model));
}


I can't seem to check multiple scenarios, it will only check one at a time.
0

#11 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 16 July 2010 - 06:06 PM

You need to call $model->validate() after setting each scenario, otherwise it will only check the last scenario that was set.
0

#12 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 06:11 PM

View PostSniper, on 16 July 2010 - 06:06 PM, said:

You need to call $model->validate() after setting each scenario, otherwise it will only check the last scenario that was set.

That has not worked I'm afraid. It still only takes in to account the last scenario that was set.
0

#13 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 16 July 2010 - 06:26 PM

can you post the controller code?

call var_dump($model->getScenario()) after $model->setScenario() this should print a different scenario name after each call, if its blank then something is wrong with your post request!
0

#14 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 06:30 PM

The controller code is posted in an above post.

I think because both scenarios are active, $model->setScenario($scenario); just gets overwritten with the latest call!
0

#15 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 16 July 2010 - 06:34 PM

The code should have changed by now the above code will not work!!
0

#16 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 06:37 PM

OK check it then:

Rules:

array('car_make', 'required', 'on'=>'Car'),
			
array('return_datetime', 'required', 'on'=>'Home'),

Controller:

public function actionForm()
{
	$model=new Enquiry;
	
	if(isset($_POST['vehicle_type']))
	{
		$scenario=$_POST['vehicle_type'];
		$model->setScenario($scenario);
		$model->validate();
	}
	if(isset($_POST['journey_from']))
	{
		$scenario=$_POST['journey_from'];
		$model->setScenario($scenario);
		$model->validate();
	}
	
	if(isset($_POST['Enquiry']))
	{
		$model->attributes=$_POST['Enquiry'];
			
		if($model->validate())
		{
			// processing here
		}
	}
	
	$this->render('index', array('model'=>$model));
}


So in my form, $_POST['vehicle_type'] is "Car" and $_POST['journey_from'] is "Home". This should mean that both 'car_make' and 'return_datetime' fields should be required. At the moment, only 'return_datetime' field is showing as an error.
0

#17 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 16 July 2010 - 06:47 PM

Each time you call validate() it resets the previous error messages because they were for a different scenario! so you need to build an array of errors to use later.

so after each ->validate call you need to collect the errors by calling $model->getErrors() (adding them to new array $allErrors) and after the last validate set the errors again $mode->addErrors($allErrors)
0

#18 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 July 2010 - 06:58 PM

Can't get it to work! Post some code for us, modify my code above!
0

#19 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 17 July 2010 - 06:29 AM

Anyone able to advise?
0

#20 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 17 July 2010 - 11:44 AM

Can you post the code you tried with the getErrors() and addErrors() function calls.
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users