codingdaddy
codingdaddy

Reputation: 497

First name and last name validation in Yii

I have been using Yii for some time.

Here is my question. Say I have an inquiry form. The form has to have first name field and last name field.

<?php echo $form->textField($model,'first_name'); ?>
<?php echo $form->textField($model,'last_name'); ?>

But I want to show only one error message for both field with message like (Both first name and last name are required) I do not want two different error messages like the following

<?php echo $form->error($model,'first_name'); ?>
<?php echo $form->error($model,'last_name'); ?>

How can I do that? Thank you for your time.

Upvotes: 0

Views: 943

Answers (1)

ineersa
ineersa

Reputation: 3435

Make custom validation in model:

public function rules()
    {
    return array(
        array('first_name,last_name', 'my_required'),
....
}
public function my_required()
    {
if (!isset($this->first_name)||!isset($this->last_name)||$this->first_name==''||$this->last_name=='')
       $this->addError('first_name','Please provide first name and last name');//add to atribute where you want to display error
    }

Also you can do it with hasError($attribute) function. So check your fname, lname for errors and show your error manually.

Upvotes: 4

Related Questions