dwightwatson / validating

Automatically validating Eloquent models for Laravel
MIT License
968 stars 76 forks source link

model validationAttributeNames #199

Closed acodercat closed 6 years ago

acodercat commented 6 years ago

I wanna in my model like this

public $validationAttributeNames = [
        'starts_on' => 'Starts On Date',
        'ends_on' => 'Ends On Date',
    ];
dwightwatson commented 6 years ago

I don’t understand your question?

acodercat commented 6 years ago

in model I wanna define validationAttribute

dwightwatson commented 6 years ago

You can... https://github.com/dwightwatson/validating/blob/master/src/ValidatingTrait.php#L35-L40

acodercat commented 6 years ago

"App\Family\Domain\Model\FamilyMemberModel and Watson\Validating\ValidatingTrait define the same property ($validationAttributeNames) in the composition of App\Family\Domain\Model\FamilyMemberModel. However, the definition differs and is considered incompatible. Class was composed"

dwightwatson commented 6 years ago

Could be because you're using public instead of protected. You're really going to have to supply more than a fraction of a code sample of you want help

acodercat commented 6 years ago
<?php
/**
 * Created by IntelliJ IDEA.
 * User: codercat
 * Date: 2018/5/21
 * Time: 17:31
 */

namespace App\Family\Domain\Model;

use Watson\Validating\ValidatingTrait;
use App\Common\Model\BaseModel;
use App\Family\Domain\Exceptions\DuplicateBindException;

class FamilyMemberModel extends BaseModel
{

    use ValidatingTrait;

    protected $table = 'family_member';
    protected $fillable = ['owner_id', 'age', 'owner_relationship', 'sex', 'mobile', 'fullname','id_card_no','health_card_no','is_auth'];

    protected $rules = [
        'mobile'   => 'bail|required|mobile',
        'fullname'   => 'bail|required',
        'sex'   => 'bail|required|in:male,female',
        'age'   => 'bail|nullable|integer|between:0,130',
        'owner_relationship'   => 'bail|nullable|in:self,children,other,husband,wife,mother,daughter,father',
        'id_card_no'   => 'bail|nullable|id_card_no|unique:family_member',

    ];

    protected $validationAttributeNames = [
        'mobile' => '手机号',
        'fullname' => '姓名',
    ];
}
acodercat commented 6 years ago

thanks

dwightwatson commented 6 years ago

Did switching to protected solve the issue? If not could you provide the complete stack trace of the error.

acodercat commented 6 years ago

How to do switching to protected

acodercat commented 6 years ago

$validationAttributeNames is protected

dwightwatson commented 6 years ago

In your initial example you had it as public, when it was supposed to be protected. Now that you've changed it to protected does the problem you were having still persist?

acodercat commented 6 years ago

error :"App\Family\Domain\Model\FamilyMemberModel and Watson\Validating\ValidatingTrait define the same property ($validationAttributeNames) in the composition of App\Family\Domain\Model\FamilyMemberModel. However, the definition differs and is considered incompatible. Class was composed"

dwightwatson commented 6 years ago

Cool, I understand what you're running into now. Found the issue and pushed a fix in 3.1.4. It might take a little while to become available on Composer but when you pull in the new version you should be good to go. Let me know if it doesn't resolve the issue for you.