모델의 두 속성 값을 비교해야하며 첫 번째 값이 두 번째 값보다 낮은 경우에만 양식을 확인할 수 있습니다. 아래 코드로 시도하지만 작동하지 않습니다.
컨트롤러
public function actionOpanningBalance(){
$model = new Bill();
if ($model->load(Yii::$app->request->post())) {
$model->created_at = \Yii::$app->user->identity->id;
$model->save();
}else{
return $this->render('OpanningBalance', [
'model' => $model,
]);
}
}
모델
public function rules()
{
return [
[['outlet_id', 'sr_id', 'bill_number', 'bill_date', 'created_at', 'created_date','bill_amount','credit_amount'], 'required'],
[['outlet_id', 'sr_id', 'created_at', 'updated_at'], 'integer'],
[['bill_date', 'd_slip_date', 'cheque_date', 'created_date', 'updated_date','status'], 'safe'],
[['bill_amount', 'cash_amount', 'cheque_amount', 'credit_amount'], 'number'],
[['comment'], 'string'],
['credit_amount',function compareValue($attribute,$param){
if($this->$attribute > $this->bill_amount){
$this->addError($attribute, 'Credit amount should less than Bill amount');
}],
[['bill_number', 'd_slip_no', 'bank', 'branch'], 'string', 'max' => 225],
[['cheque_number'], 'string', 'max' => 100],
[['bill_number'], 'unique']
];
}
}
유효성 검사기 기능에 들어가고 싶지만 원하는 오류를 추가하지 않습니다
$this->addError($attribute, 'Credit amount should less than Bill amount');
누구든지 도와 줄 수 있나요?
- 답변 # 1
- 답변 # 2
아래와 같이 사용할 수 있습니다. 답변도 쓰십시오
public function rules(){ return [ ['credit_amount','custom_function_validation', 'on' =>'scenario']; } public function custom_function_validation($attribute){ // add custom validation if ($this->$attribute < $this->cash_amount) $this->addError($attribute,'Credit amount should less than Bill amount.'); }
- 답변 # 3
유효성 검사에서 오류가 추가되지 않으면 대부분 생략 된 것입니다. 이 문제는 기본 규칙 동작으로 인해 발생했을 가능성이 높습니다. https://www.yiiframework.com/doc/guide/2.0/en/input-validation#inline-validators
구체적으로 :
와이즈 비즈따라서
By default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules. If you want to make sure a rule is always applied, you may configure the skipOnEmpty and/or skipOnError properties to be false in the rule declarations.
skipOnEmpty
자신에게 적합한 값 :skipOnError
- 답변 # 4
다음과 같이 3 번째 매개 변수를 사용하여 custom_function_validation을 작동 시켰습니다 :
[ ['country', 'validateCountry', 'skipOnEmpty' => false, 'skipOnError' => false], ]
public function is18yo($attribute, $params, $validator) { $dobDate = new DateTime($this->$attribute); $now = new DateTime(); if ($now->diff($dobDate)->y < 18) { $validator->addError($this, $attribute, 'At least 18 years old'); return false; } }
관련 자료
- knockout.js - jquery 버전을 21에서 3으로 업그레이드 한 후 녹아웃 유효성 검사 사용자 지정 규칙이 작동하지 않습니다
- javascript - Angular 9에서 개발 된 Custom Directive는 모바일에서 작동하지 않습니다
- ruby - rails의 사용자 지정 도메인이 apptestcom - 3000에서 작동하지 않습니다
- drupal - 일부 #states는 사용자 정의 필드에서 작동하지 않지만 동일한 웹 양식 필드에서 작동합니다
- 사용자 정의 Django SlugField 유효성 검사로 URL 일치 중단
- ionic framework - Angular를 사용하여 confirmPassword에 대한 사용자 지정 유효성 검사
- sql - Postgresql에서 작동하지 않는 현재 날짜 및 사용자 지정 시간으로 쿼리 업데이트
- drupal - managed_file 사용자 정의 양식 필드의 #ajax가 작동하지 않습니다
- reactjs - 사용자 지정 반응 후크가 작동하지 않는 입력 양식 설정
- java - @Valid가 작동하지 않는 스프링 부트 유효성 검사 주석
- ios - 앱에서는 작동하지만 위젯에서는 작동하지 않는 사용자 정의 글꼴
- typescript - nesjts에서 검증을위한 커스텀 데코레이터 - 서비스 계층에서
- c# - 사용자 지정 컨트롤을 사용하여 작동하지 않는 바인딩
- HTML에서 함수 이름이 언급되면 각도 유효성 검사가 작동하지 않습니다
- Amazon Advertising API가 S3에 대한 사용자 지정 VPC 엔드 포인트 정책과 작동하지 않습니다
- javascript - 녹아웃 사용자 지정 유효성 검사 - 관찰 가능 값이 특정 값과 같은지 확인하는 방법은 무엇입니까?
- reactjs - 양식에서 작동하지 않는 React 입력 유효성 검사
- xml - Android에서 작동하지 않는 사용자 정의 백그라운드 리소스
- mule4 - Mule ESB에서 사용자 지정 검증
- javascript - 양식의 유효성 검사 코드가 제대로 작동하지 않습니다
이것을 시도하십시오 :
확인을 위해
익명 기능을 사용할 수 있습니다 :