package com.jfinal.club._admin.ws;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName DocWsValidator
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/24 18:21
 */
public class WsAdminValidator extends Validator {


    @Override
    protected void validate(Controller c) {

        setShortCircuit(true);
        setRet(Ret.fail( ));
        c.getFile();
        validateRequiredString("wsinfo.wsRequest", "msg", "请填写诉讼请求!");
        validateRequiredString("wsinfo.wsReason", "msg", "请填写事实理由!");

        if (c.get("wsinfo.wsRequest").length( ) > 5000) {
            addError("msg", "诉讼请求不能大于5000个字符!");
        }

        if (c.get("wsinfo.wsReason").length( ) > 5000) {
            addError("msg", "事实理由不能大于5000个字符!");
        }


        String moneyReg = "^(([1-9][0-9]{0,6})|([0]{1})|(([0]\\.\\d{1,2}|[1-9][0-9]{0,14}\\.\\d{1,2})))$";
        if (c.get("wsinfo.money") !=null && !c.get("wsinfo.money").matches(moneyReg)) {
            addError("msg", "金额输入有误!");
        }

        if(c.get("wsinfo.materialName") != null && c.get("wsinfo.materialName").length() > 50){
            addError("msg", "材料名称不能大于50个字符!");
        }

        if(c.get("wsinfo.evidence") != null && c.get("wsinfo.evidence").length() > 5000){
            addError("msg", "证据内容不能大于5000字符!");
        }


    }


    @Override
    protected void handleError(Controller c) {
        Ret ret = getRet( );
        c.renderJson(ret);
    }
}
