1

Here is my Rule :

Table Name is : company_info

I have only two fields CompanyID and Telephone

In the update section, i want to check whether the Telephone Number exists for other columns and if the own field have it i don't want to check it. (Currently it checks the own data and returning with Telephone number was taken already).

'Telephone'       => 'unique:company_info',

Then i tried with the below rule

But i miss in the

'Telephone' => 'unique|unique:company_info,CompanyID,'.$companyid)

or

'Telephone' => 'unique|unique:company_info,Telephone,'.$companyid)

or

'Telephone' => 'unique|unique:company_info,Telephone,'.$Telephone)

Here is my Code :

$companyid = Input::get('CompanyID');
$Telephone = Input::get('Telephone');
$rule  =  array(
'Telephone' => 'unique|unique:company_info,CompanyID,'.$companyid
                )
$validator = Validator::make($data,$rule);

        if ($validator->fails())
        {
            $messages = $validator->messages();
         return "0"."||".$messages = $validator->messages()->first('Telephone');
        }

While the update query i need to check for the unique rule except the given id

I refered this one http://laravel.com/docs/4.2/validation#rule-unique

But i am not getting return on $validator = Validator::make($data,$rule);

How can i check for the unique value except the own column

0

3 Answers 3

1

I believe you have the wrong syntax for unique validation it should be

'Telephone' => 'unique:company_info,CompanyID,'.$companyid

or

'Telephone' => 'required|unique:company_info,CompanyID,'.$companyid

and not

'Telephone' => 'unique|unique:company_info,CompanyID,'.$companyid
Sign up to request clarification or add additional context in comments.

Comments

1

Can try this as the Laravel Validation provides us various features

$companyid = Input::get('CompanyID');
$Telephone = Input::get('Telephone');
$data   =   array('companyid'=>$companyid, 'Telephone'=>$Telephone );

//FOR INSERTING NEW DATA
$rule  =  array(
    'Telephone' => 'required|unique:company_info,Telephone,{:id}'
                );
$validator = Validator::make($data,$rule);

//FOR UPDATING AN EXISTING DATA
public static function rule ($id, $merge=[])  { 
    return array_merge(
        [
             'Telephone' => 'required|unique:company_info,Telephone,'.$id,
        ], 
    $merge);
}

$validator = Validator::make($data,self::rule($id));

Comment for errors...

Comments

0

Try following code

'Telephone' => 'unique:company_info,Telephone,'.$companyid.', CompanyID';

{rule} => 'unique:{table_name},{unique_column_name},{except_column_value},{except_column_name}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.