入力規則とカスタムコントローラ
カスタムコントローラを使用する Visualforce ページにユーザがデータを入力し、そのデータが入力規則エラーになった場合、エラーが Visualforce ページに表示されることがあります。標準コントローラを使用するページと同様に、入力規則エラーの場所が <apex:inputField> コンポーネントに関連付けられた項目の場合、エラーはそこに表示されます。入力規則エラーの場所がページ上部に設定されている場合は、<apex:page> 内の <apex:messages> コンポーネントを使用してエラーを表示します。ただし、ページに情報を取得するには、カスタムコントローラが例外をキャッチする必要があります。
たとえば、次のページがあるとします。
次のようなカスタムコントローラを記述する必要があります。
ユーザがページを保存したときに、入力エラーがトリガされると、標準コントローラの場合��同様に、例外がキャッチされ、ページに表示されます。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="MyController" tabStyle="Account">
18 <apex:messages/>
19 <apex:form>
20 <apex:pageBlock title="Hello {!$User.FirstName}!">
21 This is your new page for the {!name} controller. <br/>
22 You are viewing the {!account.name} account.<br/><br/>
23 Change Account Name: <p></p>
24 <apex:inputField value="{!account.name}"/> <p></p>
25 Change Number of Locations:
26 <apex:inputField value="{!account.NumberofLocations__c}" id="Custom_validation"/>
27 <p>(Try entering a non-numeric character here, then hit save.)</p><br/><br/>
28 <apex:commandButton action="{!save}" value="Save New Account Name"/>
29 </apex:pageBlock>
30 </apex:form>
31</apex:page>1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class MyController {
18 Account account;
19
20 public PageReference save() {
21 try{
22 update account;
23 }
24 catch(DmlException ex){
25 ApexPages.addMessages(ex);
26 }
27 return null;
28 }
29
30 public String getName() {
31 return 'MyController';
32 }
33
34 public Account getAccount() {
35 if(account == null)
36 account = [select id, name, numberoflocations__c from Account
37 where id = :ApexPages.currentPage().getParameters().get('id')];
38 return account;
39
40 }
41}