�쐬 2004/4/26
�o���f�[�V�����̃���
�o���f�[�V�����́A���͒l�̃`�F�b�N�̎d�g�݂ł��B �o���f�[�V�����̓o���f�[�^�N���X�A���邢�̓o���f�[�g���\�b�h���w�肷�邱�ƂŎ��s���܂��B �f�t�H���g�̃o���f�[�g�N���X������������܂��B
���̂R��ނ̃o���f�[�^��JSF�Ƀf�t�H���g�őg�ݍ��܂�Ă���A JSP�^�O���Ɏw�肷�邾���ŗ��p�ł��܂��B
�N���X | �^�O |
---|---|
DoubleRangeValidator | validateDoubleRange |
LengthValidator | validateLength |
LongRangeValidator | validateLongRange |
�Ⴆ�A������̒������`�F�b�N����LengthValidator�͎��̂悤�Ɏg�p���܂��B
<h:form> <p>a: <h:inputText value="#{hoge.s}" <f:validateLength maximum="8"/> </h:inputText> <p><h:commandButton value="GOGO" action="val2"/> <p><h:messages/> </h:form>
���s���ʂ͈ȉ��̂悤�ɂȂ�܂��B�o���f�[�V�����ŁA�G���[�ɂȂ����ꍇ�́A ���̉�ʂɑJ�ڂ���(action�͎��s���ꂸ)�A�o���f�[�V�����̃G���[���b�Z�[�W���\������܂��B
�K�{�`�F�b�N�́A�P����required="true"���w�肷�邾���ł��B
<h:inputText value="#{hoge.s}" required="true"/>
UIInput�̃^�O��validate�����Ƀo�b�L���O�r�[���̃��\�b�h���w�肷�邱�ƂŁA ���\�b�h�ɂ�鎩��̃��\�b�h�ɂ��o���f�[�V�������s�����Ƃ��ł��܂��B
<h:inputText value="#{hoge.s}" validator="#{hoge.validateS}"/>
���\�b�h�͎��̂悤�Ȉ����Œ�`���܂��B
public void validateS(FacesContext context, UIComponent component, Object value){ String text = value.toString(); if(text.equals("shit")){ context.addMessage(component.getClientId(context), new FacesMessage("���i�̓_���ł�")); ((UIInput)component).setValid(false); } }
���̗�ł́A"shit"�Ƃ����������͒l�ɓ��͂��ꂽ�Ƃ��A�o���f�[�V�����G���[�ɂ��Ă��܂��B �G���[�ɂ���ɂ́AFacesContext#addMessage(...)�ŃG���[���b�Z�[�W��ݒ肵�AUIInput#setValid(...)�� �Ó�����false��(�o���f�[�V���������s������Ԃ�)�w�肵�Ă܂��B
setValid������@�̑��ɁA�ȉ��̂悤��ValidatorException��throws���Ă����l�̂��Ƃ��s���܂��B
public void validateS(FacesContext context, UIComponent component, Object value){ String text = value.toString(); if(text.equals("shit")){ throw new ValidatorException(new FacesMessage("���i�̓_���ł�")); } }
�o���f�[�V���������Ɋ֘A����̂ł͂���܂��AJSF�ł̃��b�Z�[�W���\�[�X�̎�舵���ɂ��ĊȒP�Ƀ������Ă����܂�(�\��)�B
@TODO
���b�Z�[�W�̒lj��ƕ\��
�w�肵���ꏊ�Ƀ��b�Z�[�W���o��
���b�Z�[�W�̓��{�ꉻ
�O���[�o�����b�Z�[�W
�o���f�[�^�̓N���X�Ƃ��ĐV���ɒ�`�A���p���邱�Ƃ��ł��܂��B
���̃��X�g�̓J�X�^���o���f�[�^�N���X�̗�ł��B �o���f�[�^�N���X��Validator�C���^�[�t�F�C�X���������č쐬���܂��B validate���\�b�h�̒��g�́A�����قǂ̃��\�b�h�̃o���f�[�^�ƂقƂ�Ǔ����ł��B
public class BannedWordValidator implements Validator{ public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { String text = value.toString(); if(text.equals("shit")){ throw new ValidatorException(new FacesMessage("���i�̓_���ł�")); } } }
�����݂��������܂����B��������StateHolder����������K�v������悤�ł�(�������Ȃ��ƍ����ʂ�����)�B�Ⴆ��STATE_SAVING_METHOD��client�ɂ��Ă���Ƃ��ARegExpValidator.pattern�̒l��null�ɂȂ�܂��BValidator�Ɍ��炸�A�J�X�^������ʂ���Ƃ��͑S���ł��ȁB
�쐬�����o���f�[�^��JSP�����痘�p����ɂ́Afaces-config.xml�֓o�^���܂��B
<validator> <validator-id>BannedWordValidator</validator-id> <validator-class>hoge.BannedWordValidator</validator-class> </validator>
JSP���͎��̂悤�ɂȂ�܂��B<f:validator>�^�O�𗘗p���܂��B validatorId�ɂ�faces-config.xml�ɓo�^����id���w�肵�܂��B
<h:inputText value="#{hoge.s}"> <f:validator validatorId="BannedWordValidator"/> </h:inputText>
��̗�ł́AJSP����o���f�[�^�ɉ��炩�̃p�����[�^���킽���܂���B �p�����[�^��n�������ꍇ�́A�o���f�[�^�p�̃J�X�^���^�O���쐬���܂��B
�����ō쐬����o���f�[�^�͐��K�\���`�F�b�N�o���f�[�^�ł��B���K�\���̃p�^�[���� �Z�b�g�ł���悤�ɂ��Ă��܂��B
public class RegExpValidator implements Validator{ private String pattern; public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { String text = value.toString(); if(!text.matches(pattern)){ throw new ValidatorException(new FacesMessage("�������Ⴄ����")); } } public void setPattern(String pattern){ this.pattern = pattern; } }
���̃o���f�[�^��faces-config.xml�ɓo�^���܂��B�����܂ł́A�O�̗�ƂقƂ�Ǔ����ł����A attribute�����ɐݒ�ł���p�����[�^���w�肵�Ă���Ƃ��낪��قȂ�܂��B
<validator> <validator-id>RegExpValidator</validator-id> <validator-class>hoge.RegExpValidator</validator-class> <attribute> <attribute-name>pattern</attribute-name> <attribute-class>java.lang.String</attribute-class> </attribute> </validator>
���̃o���f�[�^�ɑ���J�X�^���^�O�͎��̂悤�ɂȂ�܂��B �R���X�g���N�^�Ńo���f�[�^��ID���Z�b�g���AcreateValidator()�ŁA�o���f�[�^�̃C���X�^���X���쐬���Ă��܂��B
public class RegExpValidatorTag extends ValidatorTag{ private String pattern; public RegExpValidatorTag(){ setValidatorId("RegExpValidator"); } protected Validator createValidator() throws JspException { //RegExpValidator validator = new RegExpValidator(); RegExpValidator validator = (RegExpValidator)super.createValidator(); validator.setPattern(pattern); return validator; } public String getPattern() { return pattern; } public void setPattern(String pattern) { this.pattern = pattern; } }
TLD�t�@�C���͂����Ȃ�܂��B
<tag> <name>validateRegExp</name> <tag-class>hoge.RegExpValidatorTag</tag-class> <attribute> <name>pattern</name> <required>true</required> </attribute> </tag>
JSP���͎��̂悤�ɂȂ�܂��B
<%@ taglib uri="WEB-INF/test.tld" prefix="myh" %> ... <h:inputText value="#{hoge.s}"> <myh:validateRegExp pattern="^.+@.+$"/> </h:inputText>
Sun J2EE 1.4 Tutorial
Chapter18 �� Using the Standard Validators �� Using Custom Objects
cardemo�T���v����FormatValidator
������UI�R���|�[�l���g�̕����̃o���f�[�g�͂ǂ��ɏ����ׂ��H�A�N�V�����H (ex.���͒l�̍��v��100�|�C���g�ȉ��AA��B��菬����)